The channel-based communication pattern

As mentioned earlier the SockJS' API is generic. It doesn't force any communication pattern. What it does is provide a real-time connection between the clients and the server through WebSocket. This gives us the freedom to build the communication pattern that suits our application.

In our application, when multiple users open the same board, everything that happens on that board needs to be updated to all the other users automatically, without refreshing the page. For example, once a user adds a new card to the Todo list, that card should show up immediately in all other users' Todo lists. The communication here is a publish/subscribe pattern. The user who added the new card triggers the publishing of the new card added message on the server side. The server publishes this message to all the clients that have subscribed to this message. This message shouldn't be published to any other clients.

Besides the new card added message, we could have many other messages, for example, new card list added card position changed. If we require the clients to subscribe to each of these messages individually, there will be a lot of boilerplate code. In our application, this is unnecessary because everybody who has access to a board should be able to receive messages from that board.

So, instead of requiring the clients to subscribe to individual messages, we can create a message channel and send various messages to that channel. Clients that have subscribed to that channel will receive those messages. It is up to the clients to decide what to do with each message. A channel is simply a string that starts with /. We can name the channel whatever we want, as long as it is unique and makes sense to our application. For example, we can create a board channel called /board/11 is the ID of the board.

With the board channel, everyone who opens a board will send a message to the server to subscribe to that board channel. After the user leaves the board, the client side will send another message to unsubscribe from that board channel.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.176.5