Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebSocket app architecture

Tags:

java

websocket

Let's consider application using WebSockets which can be divided into several, independent modules. A simplest example would be chat application where client app can join/connect to several chat rooms at once (each chat room is independent from each other). What is the preferred aproach of organizing the connections while developing such application

  1. Open new websocket connection in client for each chat room. This way you'll have multiple instances of javax.websocket.server.ServerEndpoint on the server side, each with different url. Both the server and client apps will thus be a little bit less complex and can be separated into functional (reusable) blocks. The drawback is that the client will have to keep multiple opened connnections at once. In my case we're talking about up to ten max at a time.

  2. Open one websocket connection and multiplex the messages to chat room underneath, i.e by field with chat room id in the messages. Not a big deal to implement, will make the app a little bit more complex, but is it worth it?

What is the preferred approach?

like image 708
Mariusz Jamro Avatar asked Nov 01 '22 15:11

Mariusz Jamro


1 Answers

This is not easy to answer generally, for it depends on your specific setup. However, here are my thoughts on this:

I think option 2 is the better approach, because open connections are really a limited resource for many webservers. Remember, that a websocket connection is different from a regular http request and stays open over a long time. The additional complexity of the multiplexing protocol is really not an issue I think. All implementations I know of websocket communication protocols use the latter approach, although I must admit to not know really many examples.

like image 82
luksch Avatar answered Nov 15 '22 05:11

luksch