1io.on("connection", (socket) => {
2
3 // sending to the client
4 socket.emit("hello", "can you hear me?", 1, 2, "abc");
5
6 // sending to all clients except sender
7 socket.broadcast.emit("broadcast", "hello friends!");
8
9 // sending to all clients in "game" room except sender
10 socket.to("game").emit("nice game", "let's play a game");
11
12 // sending to all clients in "game1" and/or in "game2" room, except sender
13 socket.to("game1").to("game2").emit("nice game", "let's play a game (too)");
14
15 // sending to all clients in "game" room, including sender
16 io.in("game").emit("big-announcement", "the game will start soon");
17
18 // sending to all clients in namespace "myNamespace", including sender
19 io.of("myNamespace").emit("bigger-announcement", "the tournament will start soon");
20
21 // sending to a specific room in a specific namespace, including sender
22 io.of("myNamespace").to("room").emit("event", "message");
23
24 // sending to individual socketid (private message)
25 io.to(socketId).emit("hey", "I just met you");
26
27 // WARNING: `socket.to(socket.id).emit()` will NOT work, as it will send to everyone in the room
28 // named `socket.id` but the sender. Please use the classic `socket.emit()` instead.
29
30 // sending with acknowledgement
31 socket.emit("question", "do you think so?", (answer) => {});
32
33 // sending without compression
34 socket.compress(false).emit("uncompressed", "that's rough");
35
36 // sending a message that might be dropped if the client is not ready to receive messages
37 socket.volatile.emit("maybe", "do you really need it?");
38
39 // sending to all clients on this node (when using multiple nodes)
40 io.local.emit("hi", "my lovely babies");
41
42 // sending to all connected clients
43 io.emit("an event sent to all connected clients");
44
45});