Node.js primus websocket clustering

778 views Asked by At

Trying most of the websocket engines I've concluded that best way is using Primus (a universal wrapper for real-time frameworks) so to be able to test any websocket framework that may come around without changing my functionality.

Even though that Primus does what it says, i've found myself in the situation were I wanted to scale .

Primus has many plugins and two of those are : primus-cluster and primus-redis-rooms. These two are the ones that use Redis pub-sub in order to scale when you have many node processes. The problem that I faced with both plugins is that I cannot send a message to an individual socket - spark . Meaning that sparks are not saved - passed to Redis so that each process knows how many sparks are in total .

Does anyone have an idea on how to implement this?

1

There are 1 answers

0
3rdEden On

The problem with primus-redis and primus-redis rooms is that only implements broadcasting and not from one server -> a spark on a different server messaging.

As for the rooms hack that you suggest is an "ok" alternative but it's definitely a hack and provides a lot of overhead. I don't think it's that hard to create a plugin which:

  • adds the spark.id to redis (spark.id -> server address) for each connection that it accepts.
  • removes the spark.id from redis when the connection disconnects.
  • adds pub/sub channel (server address) for the server so it can receive messages.
  • make this channel listen to messages with spark.ids and finds the sparks on the Primus server and writes the message.
  • write a method that finds the spark.id in redis, so it knows the server address and does a PUBLISH to the channel with the message that needs to be written together with the spark.id.
  • publish module to npm and receive a lot of free beer ;-)

It might take a bit longer to write then the hack you suggested but It would probably be worth the effort.