Skip to content

Instantly share code, notes, and snippets.

@crtr0
Created June 8, 2012 17:02
Show Gist options
  • Select an option

  • Save crtr0/2896891 to your computer and use it in GitHub Desktop.

Select an option

Save crtr0/2896891 to your computer and use it in GitHub Desktop.
A simple example of setting-up dynamic "rooms" for socket.io 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 Socket.io 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) {
socket.join(room);
});
});
// now, it's easy to send a message to just the clients in a given room
room = "abc123";
io.sockets.in(room).emit('message', 'what is going on, party people?');
// this message will NOT go to the client defined above
io.sockets.in('foobar').emit('message', 'anyone in this room yet?');
@zahidtariq

Copy link
Copy Markdown

A great example, simple and useful. Thank you sir.

Can YOu tell me that how we can randomly adjust the sockets to different rooms at the same time not one room?

@jeffmanful

Copy link
Copy Markdown

Thanks 👍

@tylim88

tylim88 commented Apr 13, 2019

Copy link
Copy Markdown

this example make thing simpler

@bayraktugrul

Copy link
Copy Markdown

great example thanks!

@ldilshan

ldilshan commented May 9, 2019

Copy link
Copy Markdown

Anyone can tell me how to assign users from the db or active users in to specific Room. Where can we handle that?

thanks

@uplankita

Copy link
Copy Markdown

Thank you for a great example

@gudu1998

gudu1998 commented May 5, 2020

Copy link
Copy Markdown

This is definitely helpful!
Socket.io's docs don't provide much info about this.

Yeah socket.io docs is very bad .For basic knowledge of socket.io tutorial point is best

@FrankDev-327

Copy link
Copy Markdown

server.js line 13 room = "abc123"; - is it hardcode? if so, it's seems to me stupid.

it's an example.

@suprth

suprth commented Nov 17, 2020

Copy link
Copy Markdown

Many thanks!

@0xPranavDoshi

Copy link
Copy Markdown

Any idea on how to add other users to the room using their socketid.

@aandrade01

Copy link
Copy Markdown

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.

Thanks

@vivsh1999

Copy link
Copy Markdown

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

@Roman-Vala

Roman-Vala commented Apr 3, 2022

Copy link
Copy Markdown

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
as socket.handshake.query.room;
then join the appropriate room.

@C0rellana

Copy link
Copy Markdown

Thanks!!

@SulaimanAminuBarkindo

Copy link
Copy Markdown

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.

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

@milindmore

Copy link
Copy Markdown

Nice Tutorial, video is too good .

@LakshmiPriyaSundaram31

Copy link
Copy Markdown

Is possible create dynamic rooms in socket nodejs.
Example : If i have array of socket Ids, How can I add this socket ids in that room

@SalamonJesus1905

Copy link
Copy Markdown

if i disconnect the socket , how will i know the past activities,

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