Skip to content

Instantly share code, notes, and snippets.

Created June 8, 2012 17:02
Star You must be signed in to star a gist
What would you like to do?
A simple example of setting-up dynamic "rooms" for clients to join
// set-up a connection between the client and the server
var socket = io.connect();
// let's assume that the client page, once rendered, knows what room it wants to join
var room = "abc123";
socket.on('connect', function() {
// Connected, let's sign-up for to receive messages for this room
socket.emit('room', room);
socket.on('message', function(data) {
console.log('Incoming message:', data);
// attach to our HTTP server
io = socketio.listen(server);
// handle incoming connections from clients
io.sockets.on('connection', function(socket) {
// once a client has connected, we expect to get a ping from them saying what room they want to join
socket.on('room', function(room) {
// now, it's easy to send a message to just the clients in a given room
room = "abc123";'message', 'what is going on, party people?');
// this message will NOT go to the client defined above'foobar').emit('message', 'anyone in this room yet?');
Copy link

How can i join room at backend with io instance and not socket instance?

Copy link

Roman-Vala commented Apr 3, 2022

How can i join room at backend with io instance and not socket instance?

Hi, I think you can, if you put your room you want to join in in the query (room:"abc123") then your server will read it upon connection
then join the appropriate room.

Copy link


Copy link

In the client, how to receive only the messages of the room that was created? Because in the way you gave as an example, the client receives any message and not just from the created room.

The client will get the roomId from the server so the client will output the message to room with the roomId

Copy link

Nice Tutorial, video is too good .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment