Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node: Scale socket.io / nowjs - scale across different instances

Before starting to write my application I need to know what to do when a single node.js instance (express and (socket.io or nowjs)) isn't enough anymore.

You might tell me now, that I shouldn't care about scale until it's about time but I don't want to develop an application and run into trouble because you can't easily scale socket.io or nowjs across multiple instances.

I recently read that socket.io now supports a way to scale using Redis (which I also have no experience in). Nowjs is build on to of socket.io - does it work the same way? On nowjs.org you can read that a "distributed version of NowJS" is under development and is going to cost money.

like image 857
Eliasdx Avatar asked Nov 01 '11 19:11

Eliasdx


3 Answers

If you need to scale node, the first place people usually start is putting a load balancer in front of multiple node instances. The standard for this today is nginx, though I would would like to check out the node balancer 'bouncy' that came out recently. Here's an example of someone using the nginx reverse proxy to manage multiple node instances:

Node.js + Nginx - What now?

The second thing you mention is socket.io/nowjs. Depending on how you're using these frameworks, you could get into a situation where you want to share context between clients who are hitting multiple node.js instances. If this is the case, I would recommend using a persistent store, like redis, to bridge the gap between your node instances. Here's an example:

How to reuse redis connection in socket.io?

Hopefully this is enough information and reading to get you started, let me know if you have any questions.

Happy coding!

like image 157
Justin Beckwith Avatar answered Oct 28 '22 19:10

Justin Beckwith


Another useful link on 'Scaling Socket.IO' https://github.com/dshaw/talks/tree/master/2011-10-jsclub (slides and sample application)

like image 21
alessioalex Avatar answered Oct 28 '22 18:10

alessioalex


Just as a sidenote on the discussion to use nginx for reverse proxy with socket.io, the way I understand it at least, nginx 1.0.x which is stable version does not support proxying of http/1.1 connections (which is needed in order to make socket.io work with websockets). there is a workaround described on here: http://www.letseehere.com/reverse-proxy-web-sockets to make it work, or use something like this: https://github.com/nodejitsu/node-http-proxy instead, the guys at nodejitsu says this should support it.

like image 24
Allan Lund Hansen Avatar answered Oct 28 '22 19:10

Allan Lund Hansen