I am using Ratchet (http://socketo.me/) for websockets in my PHP application. I have multiple virtual machines running the application and each of these machines also host the websocket service. Requests are passed to one of these virtual machines through HAProxy. Also currently the user uses the websocket service on the same node it connects for the application.
Now I have a problem with the websocket servers discussing with each other. For example:
- user1 connects to node1 and user2 connects to node2
- User1 sends websocket message through websocket to user2
- User2 never gets this message because he is connected to another node
What are good practices doing this kind of messaging? I have some ideas but not sure what way to go:
- Each client connects to each websocket node.
- Servers publish the messages to all other nodes as well
- Use database to synchronize the messages between nodes
Both 1st and 2nd options are not very scalable if new nodes are attached to the cloud. 3rd option will cause some lag because it is necessary to check if messages from other nodes are in database in loop.
Are there any other options to handle situations like this? Thanks for the answers!