I have a site that has few thousands visits every month and its growing. I am adding new functionalities to my web that are interactive.
Now i am dealing with question, should i use one websocket connection for all functions or should i create new websocket connection for every interactive function of my app?
I am thinking about performance issues. Creating websocket means i have to save the connection on server. But multiple connection for one user = the amount of connection i have to save is much larger if i create new connection for every functionality.
What is the riht way how to deal with this?
You should have only one websocket connection, there is no reason to use multiple websockets.
From my personal experience:
Best design would be to use a REST API to handle actions comming from client and use Websocket only for notifications. using REST would manage the parsing for you.
Anyway, if you want all your app to communicate through websocket protocol, you can send through the socket something like
Then handle it in your server side using a switch case statement:
Or you can even do (no parsing needed):
What I want to say is: you don't need to open multiple websockets to solve performance issues.
By opening multiple websocket for same client your code will be hardly maintainable and you will lost the DRY principle.
Take a look at that answer, which not recommend to open multiple sockets.
And this answer which enumarate all the case where you may want to open multiple websockets for same client (pay attention, that it says that uses case are not commons)