Clustering an Express/Socket.io Server with PM2

Cam White
Cam White
Apr 10, 2018 · 2 min read

PM2, it’s a great piece of software and always getting better. One advantage to running your server daemon with it is built in load balancing, easily enabled by the -i flag. Load balancing, if you’re not familiar is the process of sending server load to different instances of the app running in a cluster. This allows you to take better advantage of your multi-core cpu by sending the incoming requests, to each, in a balanced manor. As easy as it can be, when running a more complicated setup with websockets in the mix, it’s a bit more involved in my experience.

Socket.io needs to make use of the speedy key value store redis to keep track of connections across server instances.

  • Install and start redis
# on mac
brew install redis
sudo brew services start redis
  • Install the socekt.io redis adapter in project root
npm i -S socket.io-redis

The PM2 load balancer prefers the websocket protocol over http polling, which can be configured easily.

  • The following snippet accomplishes this on the server where you would pass in the listening express server
const sockets = require('socket.io')
const redis = require('socket.io-redis')
const bindListeners = (io) => {
io.on('connection', (socket) => {
console.log(`${socket.id} connected`)
})
}
module.exports = (server) => {
const io = sockets(server, {
transports: [ 'websocket', 'polling' ]
})
// Add the redis adapter
io.adapter(redis({ host: 'localhost', port: 6379 }))
bindListeners(io)
}
  • And for the connection call on the client-side
const socket = io.connnect({
transports: [ 'websocket', 'polling' ]
})

At this point you should be all set to run run PM2 with its built in load balancer!

  • Start PM2 with the-i arguement being the number of cores you want to use
pm2 start server -i 4

This will spawn four instances of the server running in a cluster. Now when one instance becomes overloaded the traffic will spill into the other instances. Gotta love Nodejs, we just increased the vertical scale of our app by many fold in 10 mins.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store