Within each Namespace, you can define arbitrary channels called “Rooms” that sockets can join
and leave
.
This is useful to broadcast data to a subset of sockets:
在每个[名称空间](https://socket.io/docs/namespaces/)中,您可以定义称为“房间”的任意通道,套接字可以“加入”和“离开”。
这对于将数据广播到套接字的子集很有用:
Joining and leaving
You can call join
to subscribe the socket to a given channel:
您可以调用join
来将套接字订阅到给定的频道:
1 | io.on('connection', socket => { |
And then simply use to
or in
(they are the same) when broadcasting or emitting:
然后在广播或发射时简单地使用to或in(它们相同):
1 | io.to('some room').emit('some event'); |
You can emit to several rooms at the same time:
1 | io.to('room1').to('room2').to('room3').emit('some event'); |
In that case, an union is performed: every socket that is at least in one of the rooms will get the event once (even if the socket is in two or more rooms).
You can also broadcast to a room from a given socket:
在这种情况下,将执行并集:至少在一个房间中的每个套接字都将获得一次事件(即使该套接字在两个或多个房间中)。
You can also broadcast to a room from a given socket:
您还可以从给定的套接字向房间广播:
1 | io.on('connection', function(socket){ |
In that case, every sockets in the room excluding the sender will get the event.
To leave a channel you call leave
in the same fashion as join
. Both methods are asynchronous and accept a callback
argument.
在这种情况下,会议室中除发送方之外的所有套接字都将获得事件。
要离开频道,您呼叫离开的方式与加入相同。 这两种方法都是异步的,并且接受回调参数。
Default room
Each Socket
in Socket.IO is identified by a random, unguessable, unique identifier Socket#id
. For your convenience, each socket automatically joins a room identified by its own id.
This makes it easy to broadcast messages to other sockets:
Socket.IO中的每个Socket都由一个随机的,不可猜测的唯一标识符Socket#id标识。 为了您的方便,每个插座会自动加入以其自己的ID标识的房间。
这使得将消息广播到其他套接字变得容易:
1 | io.on('connection', socket => { |
Sample use cases
- broadcast data to each device / tab of a given user
- 将数据广播到给定用户的每个设备/选项卡
1 | io.on('connection', async (socket) => { |
- send notifications about a given entity
- 发送有关给定实体的通知
1 | io.on('connection', async (socket) => { |
Disconnection
Upon disconnection, sockets leave
all the channels they were part of automatically, and no special teardown is needed on your part.
You can fetch the rooms the Socket was in by listening to the disconnecting
event:
断开连接后,套接字会自动离开它们所属的所有通道,您无需进行任何特殊拆卸。
您可以通过侦听断开连接事件来获取套接字所在的房间:
1 | io.on('connection', socket => { |
Sending messages from the outside-world
In some cases, you might want to emit events to sockets in Socket.IO namespaces / rooms from outside the context of your Socket.IO processes.
There are several ways to tackle this problem, like implementing your own channel to send messages into the process.
To facilitate this use case, we created two modules:
By implementing the Redis Adapter
:
在某些情况下,您可能想从Socket.IO进程的上下文外部向Socket.IO命名空间/房间中的套接字发出事件。
有多种方法可以解决此问题,例如实现自己的渠道以将消息发送到流程中。
为了简化此用例,我们创建了两个模块:
套接字
套接字发射器
通过实现Redis适配器:
1 | const io = require('socket.io')(3000); |
you can then emit
messages from any other process to any channel
1 | const io = require('socket.io-emitter')({ host: '127.0.0.1', port: 6379 }); |
Caught a mistake? Edit this page on GitHub