Publish subscription with nodes and redis (node_redis)

I am trying to create a shared publish / subscribe server with nodejs and node_redis nodes that receive requests from a browser with a channel name and respond to any data that was also published on this channel. To do this, I use long polling requests in the browser and process these requests by sending a response when a message is received on the channel.

For each new request, an object is created for subscribing to the channel (if and only if it does not already exist).

clients = {}; //when request comes in, clients[channel] = redis.createClient(); clients[channel].subscribe(channel); 

Is this the best way to handle subscription channels, or is there another intuitive way?

+4
source share
4 answers

This seems like a pretty reasonable solution to me. What do not you like?

Something to keep in mind is that you can have multiple signatures for each Redis connection. This can ultimately complicate your logic, which is the opposite of what you are asking for. However, on a scale this may be required. Each Redis connection is relatively inexpensive, but it requires a file descriptor and some memory.

+1
source

I don’t know what your design is, but you can subscribe with one redis client on several channels (after you subscribe with a client, you can only subscribe to another channel or unsubscribe in this connection: http://redis.io / commands / subscribe ), because after receiving the message you have the full information that this message is associated with. You can then distribute this message to all interested customers.

This helped me a bit, because I could put the message type in the channel name, and then dynamically select the action for each message from a small function instead of generating a separate subscription for each channel with separate logic.

Inside my node.js server, I only have 2 redis clients:

  • simple client for all standard actions - lpush , sadd , etc.
  • subscribe client - which listens for messages on subscription channels, these messages are distributed to all sessions (stored as sets for each type of channel) using the first redis client.
+4
source

I would like to point you to my post on pubsub using socket.io along with redis. Socket.io - a very good library =>

How to use redis PUBLISH / SUBSCRIBE with nodejs to notify clients when data values ​​change?

I think the design is very simple, and it should also be very scalable.

+2
source

Full Redis Pub / Sub Example (real-time chat using Hapi.js and Socket.io)

We tried to understand Redis Publish / Subscribe ("Pub / Sub"), and all existing examples were either outdated, too simple, or lacking tests. So, we wrote Full Real-Time Chat using Hapi.js + Socket.io + Redis Pub / Sub Example with End-to-End Tests !

https://github.com/dwyl/hapi-socketio- redis-chat example p>

Build status Test coverage Code Climate Dependency Status devDependency Status

The Pub / Sub component is just a few lines of node.js code: https://github.com/dwyl/hapi-socketio-redis-chat-example/blob/master/lib/chat.js#L33-L40

Instead of embedding it here (without any context), we recommend that you check / try the example.

We built it using Hapi.js , but the chat.js file chat.js disconnected from Hapi and can be easily used with the base node.js http server or express (etc.)

0
source

Source: https://habr.com/ru/post/1341618/


All Articles