Search code examples
javaspringwebsocketspring-websocket

Purpose of message brokers in websocket communication?


I am implementing websockets for collaborative editing. For that I am using Spring 5 websockets.

The simplest example would be, two web clients are connected via websockets to my server. User 1 does some action which creates an event and sends this event info to my server. Now this event has to be sent to User 2 so that they can do appropriate UI changes.

I have two questions here:

  • Since there will be multiple instances running of this server, User 1 might connect to Server 1 and User 2 might connect to server 2. In this case how would the changes done by User 1 go to User 2 ?
  • Also, I was following this tutorial. This tutorial is implementing websockets without any message broker some tutorials are additionally using a message broker (amqp mostly). What is the point of a message broker in this case ? Is it only used because there might be too many messages and the server would be processing them one by one ?

Just wanted to add: We cannot get away with a peer to peer connection on the client side as the server needs to store the data for future.


Solution

  • By default spring 5 uses in memory simple stomp broker for all connections If you want to scale horizontally you need a message broker like RabbitMQ etc. Lets imagine the situation user1 and user2 is connected to server1 and user3 is connected to server2 . When user1 sends a message to user3 it would not be aware of user3 because server1 does not know about user3. If we have a broker this issue will be solved. So scalability is needed to handle the load and in production you always will have more than 1 instance for high availability and fault tolerance.