Publish subscribe with nodejs and redis(node_redis)

别来无恙 提交于 2020-01-22 15:35:06

问题


I am trying to build a generic publish/subscribe server with nodejs and node_redis that receives requests from a browser with a channel name and responds with any data that has been published too that channel. To do this, I am using long polling requests from the browser and dealing with these requests by sending a response when a message is received on a channel.

For each new request, an obect 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 deal with the subscribtion channels, or is there some other more intuitive way?


回答1:


That seems like a pretty reasonable solution to me. What don't you like about it?

Something to keep in mind is that you can have multiple subscriptions on each Redis connection. This might end up complicating your logic, which is the opposite of what you are asking for. However, at scale this might be necessary. Each Redis connection is relatively inexpensive, but it does require a file descriptor and some memory.




回答2:


I don't know what's your design, but you can subscribe with one redis client on multiple channels (after you subscribe with client, then you can only subscribe to other channel or unsubscribe within this connection: http://redis.io/commands/subscribe), because after you receive message, you have full information which channel this message comes from. Then you can distribute this message to all interested clients.

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

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

  1. simple client for all standard actions - lpush, sadd and so on
  2. subscribe client - which listens for messages over subscribed channels, then this messages are distribute to all sessions (stored as sets for each channel type) using first redis client.



回答3:


I would like to point you out to my post about pubsub using socket.io together with redis. Socket.io is 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.




回答4:


Complete Redis Pub/Sub Example (Real-time Chat using Hapi.js & Socket.io)

We were trying to understand Redis Publish/Subscribe ("Pub/Sub") and all the existing examples were either outdated, too simple or had no tests. So we wrote a Complete 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

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

Rather than pasting it here (without any context) we encourage you to checkout/try the example.

We built it using Hapi.js but the chat.js file is de-coupled from Hapi and can easily be used with a basic node.js http server or express (etc.)



来源:https://stackoverflow.com/questions/5140398/publish-subscribe-with-nodejs-and-redisnode-redis

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!