Skip to content
This repository has been archived by the owner on Nov 2, 2020. It is now read-only.

Websockets inside a cluster, ok in production? #143

Closed
sofasurfa opened this issue Sep 17, 2019 · 2 comments
Closed

Websockets inside a cluster, ok in production? #143

sofasurfa opened this issue Sep 17, 2019 · 2 comments

Comments

@sofasurfa
Copy link

sofasurfa commented Sep 17, 2019

@goriunov

I have a question about PM2 cluster in production as well as ClusterWs.

I conducted an experiment by laucnhing 4 child processes using PM2 (I have 4 cores on my machine). On the client I created multiple sockets, and sent many messages to the servers. One thing I didn't expect was that Node was able to figure out what child process the socket belonged to, therefore every message sent by the client was console logged by the correct child process.

It seems like the main worker in the cluster keeps track of what sockets belong where.

So does Nodejs do this using the "cluster" module?
Also is this ok in production? and can this aslo be achived with ClusterWs (in case I decide to use it instead)?

@sofasurfa sofasurfa changed the title Question: how webscokets connect to the correct child process inside a cluster? Websockets inside a cluster, ok in production? Sep 20, 2019
@goriunov
Copy link
Member

@sofasurfa sorry for late response, had quite a bit of work during the week. Websocket is consistent connection to particular server, main worker (master) is only responsible for load balance between different child processes (per request) websocket connection is single request which will be assigned to one child process and only communicate with that child (so when you send message through websocket you will always send it to the same node js child process). That is how it suppose to work and ClusterWS uses node js clustering under the hood so it has exactly the same behaviour. If your use case does not include any client to client communication then you dont really need clusterws (but most of the cases with websocket is sending messages between different clients)

ClusterWS is good for client to client/clients communication with scaling. For example you would like to send message from one client to another and you have 2 worker processes and this 2 clients are connected to different workers so you will need to set up some kind of worker to worker communication to be able to send message from one client to another. That is where clusterws comes (in general it simplifies cross workers communication)

@sofasurfa
Copy link
Author

@goriunov Thank you so much for your answer :) Also thank you for writing this awesome library!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants