WebSocket Application Architecture

Consider an application using WebSockets, which can be divided into several independent modules. The simplest example would be a chat application in which a client application can simultaneously connect to several chat rooms (each chat is independent of each other). What is the preferred approach to organizing connections when developing such an application.

  • Open a new Internet connection in the client for each chat room. This way you will have multiple instances of javax.websocket.server.ServerEndpoint on the server side, each with a different url. Thus, server and client applications will be slightly less complex and can be divided into functional (reusable) blocks. The disadvantage is that the client will have to keep several open connections at the same time. In my case, we are talking about ten max at a time.

  • Open one connection on the network and multiplex the messages into the chat room below it, that is, by the field with the chat identifier in the messages. Not a big implementation, will make the application a little more complicated, but is it worth it?

What is the preferred approach?

+4
source share
1 answer

This is not easy to answer as a whole, as it depends on your particular setting. However, here are my thoughts on this:

I think option 2 is a better approach because open connections are indeed a limited resource for many web servers. Remember that a connection to websocket is different from a regular HTTP request and remains open for a long time. The extra complexity of the multiplex protocol is really not a problem, I think. All implementations that I know about communication protocols with websocket use the latter approach, although I must admit that I really do not know many examples.

+2
source

Source: https://habr.com/ru/post/1499908/


All Articles