使用nodejs和redis进行发布订阅(node_redis)
我正在尝试使用 nodejs 和 node_redis 构建一个通用的发布/订阅服务器,该服务器接收来自浏览器的带有通道名称的请求,并响应该通道已发布的任何数据。为此,我使用来自浏览器的长轮询请求,并通过在通道上收到消息时发送响应来处理这些请求。
对于每个新请求,都会创建一个对象来订阅通道(当且仅当该对象尚不存在时)。
clients = {};
//when request comes in,
clients[channel] = redis.createClient();
clients[channel].subscribe(channel);
这是处理订阅渠道的最佳方式,还是还有其他更直观的方式?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不知道你的设计是什么,但是你可以使用一个redis客户端在多个频道上订阅(使用客户端订阅后,你只能在此连接内订阅其他频道或取消订阅: http://redis.io/commands/subscribe),因为在您收到消息后,您就拥有了该消息来自哪个渠道的完整信息。然后您可以将此消息分发给所有感兴趣的客户。
这对我有一点帮助,因为我可以将消息类型放入通道名称中,然后从小函数中动态选择每条消息的操作,而不是使用单独的逻辑为每个通道生成单独的订阅。
在我的 Node.js 服务器中,我只有 2 个 Redis 客户端:
lpush
、sadd
等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:
lpush
,sadd
and so on我想向您指出我关于将 socket.io 与 Redis 一起使用的 pubsub 的文章。 Socket.io是一个非常好的库=>
如何使用redis发布/使用nodejs SUBSCRIBE 来在数据值发生变化时通知客户端?
我认为设计非常简单,而且应该具有很强的可扩展性。
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.
对我来说这似乎是一个相当合理的解决方案。你不喜欢它什么?
需要记住的是,每个 Redis 连接上可以有多个订阅。这可能最终会使你的逻辑变得复杂,这与你所要求的相反。然而,从规模来看,这可能是必要的。每个 Redis 连接相对便宜,但它确实需要文件描述符和一些内存。
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.
完整 Redis Pub/Sub 示例(使用 Hapi.js 和 Socket.io 进行实时聊天)
我们试图了解 Redis 发布/订阅(“Pub /Sub")并且所有现有的示例要么过时,要么太简单,要么没有测试。
因此,我们使用 Hapi.js + Socket.io + Redis Pub/Sub 示例和端到端测试编写了一个完整实时聊天!
Pub/Sub 组件只有几行 node.js 代码:
https:// github.com/dwyl/hapi-socketio-redis-chat-example/blob/master/lib/chat.js#L33-L40
而不是将其粘贴到此处(没有任何上下文)我们鼓励您查看/尝试示例。
我们使用 Hapi.js 构建它,但使用
chat.js
文件与 Hapi 解耦,并且可以轻松与基本 node.js http 服务器 或 一起使用>快递(等)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!
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.)