开发者

Publish subscribe with nodejs and redis(node_redis)

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?


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.


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.


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.


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

Publish subscribe with nodejs and redis(node_redis)

Publish subscribe with nodejs and redis(node_redis)

Publish subscribe with nodejs and redis(node_redis)

Publish subscribe with nodejs and redis(node_redis)

Publish subscribe with nodejs and redis(node_redis)

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.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜