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

3

11

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.

Sanford answered 1/11, 2011 at 19:54 Comment(0)
T
14

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!

Triolein answered 1/11, 2011 at 20:3 Comment(2)
Thanks, do you know if you can use nowjs with redis?Sanford
Sure you can - they're two entirely different technologies with different uses. Nowjs is going to give you the constant server-client connectivity features similar to socket.io. This is useful for things like server to client messaging, client broadcasting, etc. The classic example is building a chatroom. Redis is used more as a back end data store - it can do things like store key-value pairs, pubsub, etc. It lives outside of node as a separate process (or even on a separate machine), which makes it great for bridging multiple servers.Triolein
C
3

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

Contagium answered 4/11, 2011 at 9:6 Comment(0)
B
1

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.

Booma answered 6/2, 2012 at 11:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.