This article is outdated as many things have changed in ClusterWS. Unfortunately i don’t have enough time (at this point) to support medium articles.

Good time of the day everyone, hope you are having a great day. If you have not heard about ClusterWS you probably should check it out (and it is not just that i am a developer and trying to make my library more popular, but also it solves a big problem with scaling WebSocket applications in Node.js)

ClusterWS reminds SocketCluster, but it works a bit differently i will write separate article about differences between this two frameworks.

Probably many people will right away ask what are the benefits of using ClusterWS in your next Real Time application. The answer is pretty simple this Framework will reduce the pain of scaling Websocket (what is probably the biggest problem which exist with Websocket applications).

So how does ClusterWS scale Websocket across machines and cpu cores ?

Hope you have heard about Node.js Cluster if not please check it out before you continues because that is really tedious to explain it here.

Basically to scale Websocket across CPUs of your server we use Node.js Cluster. Which unfortunately does not have share state, therefore we developed a Broker server which is running at the background as separate worker and passes messages from one Worker to others.

I know terrible picture (painter is the reason, and may be i am a bit 😄). This schema displays basic client server connection. So for example if i want to send message from Bob to Anna we need to go from one Child to another and for that we have to use Broker. It will work like:

Bob => Child 2 => Child 1 Broker => [Child 3, Child 4] => Anna

Yes message will be send to all childs and every server child will decide if message should go farther or should be deleted.

This kind of execution chain allows to reduce Broker load as broker will be responsible only for retrieving and emitting message to every one without even thinking if there are receivers. And then every Child instance will decide about message (send it farther or delete).

This kinda thing calls Pub/Sub that is exactly what you have to use when you build communication between clients in ClusterWS. Also in ClusterWS you can use client => server communication but this thing does not really scale well 😃 (but any way there are many use cases for client => server), therefore u will have to use Pub/Sub System (which is provided in ClusterWS) to send message from server to clients or from client to client.

This is Part 1 and in next article i will explain how we scale across Machines. Hope you will like it.