Key Concepts:
-
Server
-
Client
-
Rooms
The server is able to create ‘rooms’ arbitrarily using the .join command * e.g. socket.join(‘some room’) adds the socket into a room
The server can then emit to all sockets in that room by specifying the room address in a .to + .emit(with payload)
io.on("connection", (socket) => {
socket.join("some room");
});
io.to("some room").emit("some event");
```
Notes:
* On the frontend make sure you put any immediate emits that you need inside of the initial connect(). This will stop any race conditions or issues with connections.
``` javascript
* // Initialise the socket connection
socketRef.current = io(serverURL);
// check connection
socketRef.current.on("connect", () => {
console.log('this socket is called', socketRef.current?.id)
// // tell the server you have joined the game
socketRef.current?.emit("join-game", socketRef.current?.id)
})