使用相同的 redis.createClient() 实例进行发布和订阅 [英] Using the same redis.createClient() instance for publish and subscribe
问题描述
我正在使用 redis 在 socket.io 客户端之间发布和订阅消息,当客户端连接到服务器时 (io.sockets.on('connection', function(socket){...});
) 我正在使用 redis.createClient()
创建一个 subscribe
变量,然后使用 subscribe 函数将客户端订阅到频道.
I'm working with redis to publish and subscribe messages between socket.io clients, when client connects to the server (io.sockets.on('connection', function(socket){...});
) i'm creating a subscribe
variable using redis.createClient()
and then using the subscribe function to subscribe the client to channel.
我的问题是是否有权使用相同的订阅变量来执行发布操作?或者使用 redis.createClient()
创建另一个实例来发布消息很重要,所以我将有 2 个实例,一个用于发布,一个用于订阅...
My question is if its right to use the same subscribe variable to do a publish action? or it's important to create another instance with redis.createClient()
for publishing messages so i will have 2 instances, one for publishing and one for subscribing...
谢谢
推荐答案
来自 Redis 文档:
一旦客户端进入订阅状态,除了额外的 SUBSCRIBE、PSUBSCRIBE、UNSUBSCRIBE 和 PUNSUBSCRIBE 命令外,它不应发出任何其他命令.
Once the client enters the subscribed state it is not supposed to issue any other commands, except for additional SUBSCRIBE, PSUBSCRIBE, UNSUBSCRIBE and PUNSUBSCRIBE commands.
因此,您需要两个客户端,一个用于订阅,一个用于发布(可能还有其他命令).
For this reason, you'll need two clients, one for subscribing and one for publishing (and potentially other commands).
这篇关于使用相同的 redis.createClient() 实例进行发布和订阅的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!