Sometimes, we want to create rooms in Socket.io and JavaScript.
In this article, we’ll look at how to create rooms in Socket.io and JavaScript.
How to create rooms in Socket.io and JavaScript?
To create rooms in Socket.io and JavaScript, we just call join
to join the room.
For instance, we write
io.sockets.on("connection", (socket) => {
socket.on("create", (room) => {
socket.join(room);
});
});
to listen for the room create
event on server side.
We get the room
name and call join
with it if the create
event is emitted from client side.
On client side, we write
const socket = io.connect();
socket.emit("create", "room1");
to call socket.emit
with 'create'
and 'room1'
to emit the create
event with value 'room1
‘.
Conclusion
To create rooms in Socket.io and JavaScript, we just call join
to join the room.