Wednesday, 3 May, 2017 UTC


In this article we will focus on setting up node.js cluster for is doing multiple requests to perform handshake and establish connection with a client. With a cluster those requests may arrive to different workers, which will break handshake protocol. To solve this problem we can use perfect solution based on sticky-sessions module.  This module is balancing requests using their IP address. According to documentation thus client will always connect to same worker server, and will work as expected, but on multiple processes!
Below I present my own solution based on express.js,, sticky-sessions and cluster module available in my repository on GitHub:
The short source code with amazing possibilities – You can try it and see for yourself. In remark place You can implement your own functionality for
Our server will be work as main process with forks multiple childs in background. Please take a look into blow screen:
We see one main process running on 3000 port and additional case workers processes with their id. To be sure that our server works correct on cluster we use cluster module and check what instance is master or child. Thanks to sticky module we do not have to worry about determining the optimal number of children processes.
Let’s run the browser and see how our server works from the client side.
In browser we will see what ID child process responsed on our request  and the same information we will see on console server side.. In below sample I’ve  run 5 instances of browser with connection to our server. When the number of connections will be too much for the child process then the subsequent connections will automatically be transferred to subsequent child processes.