在多个节点实例中共享 IO 对象 [英] sharing IO object in multiple node instances
问题描述
我想在两个节点实例之间共享由 socket.io 创建的房间,已经使用 redis-store 但无法在不同的节点实例上获取 io.sockets.in(ROOM_ID).emit(MESSAGE,DATA);
I want to share rooms created by socket.io between two node instances, already using redis-store but unable to get io.sockets.in(ROOM_ID).emit(MESSAGE,DATA) on different node instance;
我正在尝试 -实例 A -
I am trying - instance A -
io.set('store',...)
io.on('connection',function(socket){
socket.join(''room-ABCD);
});
广播一些东西,我可以使用
to broadcst something, I am able to use
io.sockets.in('room-ABCD').emit('event',{data:{}});
但我需要从实例 B 执行类似的广播.
but similar broadcast I need to perform from instance B.
在实例 B io.set('store')
与上面类似,但我不能使用 io.sockets.in('room-ABCD').emit('event',{data:{}})
on instance B io.set('store')
is similar to above, but I am not able to use io.sockets.in('room-ABCD').emit('event',{data:{}})
我的理解是-redisStore会把socket+room的信息放到redis里,这样其他进程就可以访问了.[如有错误请指正].现在,我不知道如何在节点的其他进程中识别和获取此信息.
My understanding is - redisStore will put socket+room information in redis so that it is accessible to other process.[please correct if I am wrong]. now, I am not getting how to identify and fetch this information in other process of node.
谢谢.
推荐答案
Answering to the queries related to the questions - one,two and this one.
我需要集中注意力的确切点是 - 加入房间.
Exact point where I need to concentrte was - Joinig the room.
我正在使用 client_socket.join("room_name")
,但它会将客户端套接字推送到房间.
I was using client_socket.join("room_name")
, but it pushes the client socket to the room.
现在,试试io.sockets.socket(client_socket.id).join("room_name")
.
这会将房间成员推送到 redis,现在它们可以在另一个节点实例中访问.
This pushes the room members to the redis and now they are accessible in another node instance.
进程 B io.sockets.in("room_name").emit("event_name",{data:"1"})
有效.
这篇关于在多个节点实例中共享 IO 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!