Giter Site home page Giter Site logo

Cluster? about sockjs-node HOT 10 CLOSED

hunterloftis avatar hunterloftis commented on August 20, 2024
Cluster?

from sockjs-node.

Comments (10)

majek avatar majek commented on August 20, 2024

sockjs-node has some internal state - mapping from sockjs session number to an internal structure (consisting of, above all others outgoing message buffer). As node cluster is basically equivalent to fork, it doesn't share this state across forks and will not work with sockjs-node. Additionally, I can't see how this state could be shared and there doesn't seem to be any option for doing a custom routing (ie: forward all requests from session A to worker B).

As a side note node clustering should work with sockjs if only websocket transports were used (as there is not much internal state stored for that).

I'm afraid you have to run multiple workers on different ports and do balancing using for example haproxy (with sticky sessions). We have a sample haproxy config prepared.

We could make sockjs-node working with node clustering, but it's probably going to be quite hacky, and an overkill for now. Is it a big deal?

from sockjs-node.

hunterloftis avatar hunterloftis commented on August 20, 2024

It's something I may contribute to sockjs - we have solved the problem before with socket.io by using redis as a pubsub server.

We could basically copy Express's Session concept - it uses a MemoryStore instance by default but can optionally (and always in production) be set to a RedisStore. Thus, when creating a new SockJS instance you could optionally pass it a RedisStore (or other store) to replace its default MemoryStore.

For real deployment we've found that Cluster is necessary (otherwise you're using only 1 of 4 or 8 possible cores).

By the way please let me thank you for writing SockJS. It's a nice solution and I hope to use more of it.

from sockjs-node.

majek avatar majek commented on August 20, 2024

Interesting! I would prefer to keep sockjs-node as thin as possible (think: don't require redis), I hope this is possible.

MemoryState? Link please :) Also, are there any other ways of sharing the state between cluster threads?

SockJS has few big users which do use multiple cores - the solution is to spawn multiple node.js processes, each using different http port and combine that using a loadbalancer. That seems to be working fine for many people. (Although, cluster may be simpler to deploy)

from sockjs-node.

majek avatar majek commented on August 20, 2024

I won't do implement clustering any time soon. Patches welcome :) (but beware, that can be hard or maybe even impossible due to the in-memory state every connection has, which would need to be shared between threads)

from sockjs-node.

paulbjensen avatar paulbjensen commented on August 20, 2024

Hi,

I stumbled across this library when researching Socket.IO and Node's cluster API:

https://github.com/fent/clusterhub

It might be of interest with regards to finding ways to share the state between cluster threads without relying on an external dependency like Redis.

from sockjs-node.

majek avatar majek commented on August 20, 2024

@paulbjensen clusterhub looks interesting. Unfortunately, it is not really suitable for SockJS.

SockJS already supports "scaling" - just deploy multiple servers and put a sticky-sessions aware load balancer in front.

We could add support for node-style clustering, but this will only complicate manner - users would have two ways of scaling: a) on local machine using node clustering b) on local machine OR on multiple machines using loadbalancer.

Although a) is more idiomatic, I think that any user seriously wanting to scale would prefer b), as it can grow outside a single box, while a) can't.

from sockjs-node.

paulbjensen avatar paulbjensen commented on August 20, 2024

@majek. Cool.

I'm currently doing what you've recommended, but instead of using HAProxy, I'm using node-http-proxy with a custom npm I've developed to do both round-robin and sticky-sessions, which proxies requests to an app server running 4 instances (1 per CPU) of a SocketStream app with SockJS.

from sockjs-node.

majek avatar majek commented on August 20, 2024

I've not yet been able to get websockets running through node-http-proxy, but I haven't tried too hard. Give me a shout if you succeed.

from sockjs-node.

konklone avatar konklone commented on August 20, 2024

node-http-proxy does support proxying Websockets, and I've verified it working on my own machine:

https://github.com/nodejitsu/node-http-proxy#proxying-websockets

from sockjs-node.

majek avatar majek commented on August 20, 2024

Good to know. Thanks

from sockjs-node.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.