Aleksei Anatskii
Aleksei Anatskii

Reputation: 307

Node.js cluster and socket.io with mongodb as store

I've got a problem to set up clustered node.js / socket.io app, with socket.io store, based not on Redis but on MongoDB. I need to mention that my app uses Mongo to store data. So I don't want to setup another DB just to make soket.io works across multiple process.

I found an option - mong.socket.io, but it looks like I can't make this work with node clusters. I'm using node: 0.10.10, with socket.io: 0.9
I'm looking for any solution that will let me use MongoDB as a store for socket.io that runs on separate processes.

Counting on you, thanks.

Upvotes: 1

Views: 3752

Answers (1)

moka
moka

Reputation: 23047

Any of clustered worker can easily access MongoDB using mongodb or mongoose in order to get required data. As well it can store that data locally as a cache.
Once socket.io client is connected it will be bound to one of workers and will not swap workers during execution.
In order to share sessions you need to create session middleware for socket.io that will ask for session details from MongoDB from one of the workers. Check this answer which explains how to access session data in socket.io from shared session details. And in order to make it clustered, instead of MemoryStorage use connect-mongo that allows to store session data within mongo and it will make session details shared over workers.

Upvotes: 7

Related Questions