Clustering an Express/Socket.io Server with PM2

Cam White
Cam White
Apr 10, 2018 · 2 min read
# on mac
brew install redis
sudo brew services start redis
npm i -S socket.io-redis
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)
}
const socket = io.connnect({
transports: [ 'websocket', 'polling' ]
})
pm2 start server -i 4

Cam White

Written by

Cam White

Autodidact developer and VIM user https://codeandcamlabs.com

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