跳到主要内容
Version: 3.x

Emit cheatsheet

Server-side

io.on("connection", (socket) => {

// basic emit
socket.emit(/* ... */);

// to all clients in the current namespace except the sender
socket.broadcast.emit(/* ... */);

// to all clients in room1 except the sender
socket.to("room1").emit(/* ... */);

// to all clients in room1 and/or room2 except the sender
socket.to("room1").to("room2").emit(/* ... */);

// to all clients in room1
io.in("room1").emit(/* ... */);

// to all clients in namespace "myNamespace"
io.of("myNamespace").emit(/* ... */);

// to all clients in room1 in namespace "myNamespace"
io.of("myNamespace").to("room1").emit(/* ... */);

// to individual socketid (private message)
io.to(socketId).emit(/* ... */);

// to all clients on this node (when using multiple nodes)
io.local.emit(/* ... */);

// to all connected clients
io.emit(/* ... */);

// WARNING: `socket.to(socket.id).emit()` will NOT work, as it will send to everyone in the room
// named `socket.id` but the sender. Please use the classic `socket.emit()` instead.

// with acknowledgement
socket.emit("question", (answer) => {
// ...
});

// without compression
socket.compress(false).emit(/* ... */);

// a message that might be dropped if the low-level transport is not writable
socket.volatile.emit(/* ... */);

});

Client-side

// basic emit
socket.emit(/* ... */);

// with acknowledgement
socket.emit("question", (answer) => {
// ...
});

// without compression
socket.compress(false).emit(/* ... */);

// a message that might be dropped if the low-level transport is not writable
socket.volatile.emit(/* ... */);

Reserved events

On each side, the following events are reserved and should not be used as event names by your application:

  • connect
  • connect_error
  • disconnect
  • disconnecting
  • newListener
  • removeListener
// BAD, will throw an error
socket.emit("disconnecting");