Setting up node.js cluster for socket.io – A simply way to performance

In this article we will focus on setting up node.js cluster for socket.io. Socket.io 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 socket.io will work as expected, but on multiple processes!

Below I present my own solution based on express.js, socket.io, sticky-sessions and cluster module available in my repository on GitHub: https://github.com/maxprog/socket.io-node.js-cluster

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 socket.io

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.

May 3rd, 2017

  • I am commenting to make you be aware of of the fantastic encounter my wife’s daughter went through going through your web page. She discovered plenty of pieces, most notably what it is like to possess an excellent giving style to let many others easily gain knowledge of selected specialized subject areas. You really exceeded my expected results. I appreciate you for displaying these useful, safe, educational and in addition unique thoughts on this topic to Mary..

  • An interesting dialogue is worth comment. I feel that it is best to write more on this topic, it won’t be a taboo topic however generally persons are not enough to talk on such topics. To the next. Cheers.

  • I am glad for writing to let you know of the amazing encounter my friend’s daughter developed reading through your site. She came to understand such a lot of pieces, most notably how it is like to possess an ideal giving heart to have folks smoothly know several extremely tough things. You really did more than our expected results. Thanks for delivering such insightful, trusted, revealing as well as fun thoughts on this topic to Gloria..