// 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?'); |
This comment has been minimized.
This comment has been minimized.
Please write example for "io.sockets.clients(room)", how can i see who in room online ?? |
This comment has been minimized.
This comment has been minimized.
Your example is so helpfull but I have a question. How can I take off a client? |
This comment has been minimized.
This comment has been minimized.
I found an ansewer of my question in this post:https://github.com/LearnBoost/socket.io/wiki/Rooms and also can help to hyperprodify with their question. Regards |
This comment has been minimized.
This comment has been minimized.
are the clients inside the room stored as Array? how can I access the list of clients in a certain room and see who they are? |
This comment has been minimized.
This comment has been minimized.
Carter, this is awesome. Is it possible to do this with Socket.io namespaces? |
This comment has been minimized.
This comment has been minimized.
Just wanted to say thank you for simplifying it! Totally get it know. |
This comment has been minimized.
This comment has been minimized.
A huge "Thank you!" from myself as well! I've been struggling to achieve this for quite some time with no result... All the other tutorials or articles I've read don't cover the basic not even nearly as well! Now I can finally advance with my project! |
This comment has been minimized.
This comment has been minimized.
For those who wonder how to leave the previous room, if a active user joins (switches to) another room, try this: server.js
|
This comment has been minimized.
This comment has been minimized.
a customer can be in more than one room ? |
This comment has been minimized.
This comment has been minimized.
Yes, users can be in any number of rooms at once |
This comment has been minimized.
This comment has been minimized.
Great example... Thanks :) |
This comment has been minimized.
This comment has been minimized.
Great. It helps a lot. Thanks. |
This comment has been minimized.
This comment has been minimized.
This is helpful! |
This comment has been minimized.
This comment has been minimized.
Check this interactive course that gives you all the keys to a good start for a complete masteration of node.js by taking all the power of it. |
This comment has been minimized.
This comment has been minimized.
Thanks for this. |
This comment has been minimized.
This comment has been minimized.
Thanks, this helped me! |
This comment has been minimized.
This comment has been minimized.
Thank you! Super helpful! |
This comment has been minimized.
This comment has been minimized.
Thanks very much for this. =) |
This comment has been minimized.
This comment has been minimized.
This is definitely helpful! |
This comment has been minimized.
This comment has been minimized.
Damm ! That Simple. |
This comment has been minimized.
This comment has been minimized.
and where is the dynamic example? |
This comment has been minimized.
This comment has been minimized.
Very useful, thanks~ |
This comment has been minimized.
This comment has been minimized.
Still useful. Thanks! |
This comment has been minimized.
This comment has been minimized.
Very useful, clear....thank you so much.. |
This comment has been minimized.
This comment has been minimized.
Thanks ! |
This comment has been minimized.
This comment has been minimized.
You make it look so easy |
This comment has been minimized.
This comment has been minimized.
Thanks :) |
This comment has been minimized.
This comment has been minimized.
Cheers! |
This comment has been minimized.
This comment has been minimized.
Thanks |
This comment has been minimized.
This comment has been minimized.
THANK YOU! |
This comment has been minimized.
This comment has been minimized.
thank you :) |
This comment has been minimized.
This comment has been minimized.
Does this work in 1.5? |
This comment has been minimized.
This comment has been minimized.
Thank you for the clear example |
This comment has been minimized.
This comment has been minimized.
thank you so much |
This comment has been minimized.
This comment has been minimized.
server.js line 13 |
This comment has been minimized.
This comment has been minimized.
@daryn-k It's only there as an example. You can swap this out for any dynamic room ID method. |
This comment has been minimized.
This comment has been minimized.
Hi everyone! |
This comment has been minimized.
This comment has been minimized.
Can we joined room permanently .... actually i am developing node + socket application as gameserver and client is unity ... and i join p1 to room when he invite to p2 ... and p2 was joined when he accept p1 invitation ! after that i had emmited to that joined room >> it works perfectly ! |
This comment has been minimized.
This comment has been minimized.
Is there any way to assign some data to a room? So that it will be shared with other sockets by reference? For example if two players were in a room and played tic tac toe and I wanted to save game board state somewhere. |
This comment has been minimized.
This comment has been minimized.
Great example! Life saver |
This comment has been minimized.
This comment has been minimized.
how to perform same operation in android |
This comment has been minimized.
This comment has been minimized.
thanks |
This comment has been minimized.
This comment has been minimized.
thanks ! |
This comment has been minimized.
This comment has been minimized.
how can we create a dynamically room for particular 2 player. |
This comment has been minimized.
This comment has been minimized.
I think this will help a lot of people, I am just using Firebase but the concept is the same, you just find somehow the identifier of your connected user and join it automatically // server side
const debugSocket = debug('debug')('myAppName:socket')
const Channel = IOServer(...)
const CustomerChannel =
Channel
.of('/customer') // I am using another channel but it doesn't not matter, just showing an example
.on('connection', async (socket)=> {
debugSocket('CustomerChannel: Connected')
let currentUser = null
// I am using firebaseToken key from the query but does not matter
// you could switch to whichever key you want as long as it is useful
// for find your user
if(socket.handshake.query && socket.handshake.query.firebaseToken) {
currentUser = await firebase.auth().verifyIdToken(socket.handshake.query.firebaseToken)
// being fancy, make sure that currentUser is not undefined or null
// or whatever your await function could have, that is a business rule
// Either Firebase callback or you just do some database lookup
// using some useful information from the token
debugSocket(`CustomerChannel:Room:Joined: ${user.email}`)
// Whatever the callback returns me I will use some idetifier
// and I will use it as the ID of my room
socket.join(user.uid)
}
socket.on('disconnect', (reason) => {
if(currentUser) {
debugSocket(`CustomerChannel:Room:Leaved: ${user.email}`)
// Same concept here I am just disconnecting the user from his own
// This is because you can still connected to CustomerChannel
// but not to your room, this depends of your use case
socket.leave(currentUser.uid)
}
})
}) In the client you need to make sure that you send the query parameter when you connect to the channel // client side
io(`/customer`, {
query: {
token: 'whatever' // just make sure you have the information before you connect to the channel
}
}) Now the second part is to send messages to specific room. // server code
// somewhere in my backend code
// I have to have the identifier of the user to be able to send him and only him messages
// so somehow I HAVE TO HAVE `user` loaded and then I will use it `uid` which was the way
// I joined the user to his room.
CustomerChannel.to(user.uid).emit('hello', { message: 'World'}) I hope you find it useful. Cheers. |
This comment has been minimized.
This comment has been minimized.
If what you want is to connect multiple people to the same room, then just send And then instead of user the After that in your backend code, somehow you HAVE TO HAVE the |
This comment has been minimized.
This comment has been minimized.
Would anyone know if rooms can only emit text? |
This comment has been minimized.
This comment has been minimized.
Thank your |
This comment has been minimized.
This comment has been minimized.
Hi, I am having a room how can I add new user to the same room in socket.io. I can't find a way please help me. |
This comment has been minimized.
This comment has been minimized.
I think you can just give the same name to any new user. |
This comment has been minimized.
This comment has been minimized.
In Socket When new connection join the room at that time its remove last connected socket from the room can you please help me on this if someone know ? Also didn't get any error when i debugging. |
This comment has been minimized.
This comment has been minimized.
Thanks! |
This comment has been minimized.
This comment has been minimized.
A great example, simple and useful. Thank you sir. |
This comment has been minimized.
This comment has been minimized.
Can anyone tell me that how we can randomly adjust the sockets to different rooms at the same time not one room? |
This comment has been minimized.
This comment has been minimized.
Can YOu tell me that how we can randomly adjust the sockets to different rooms at the same time not one room? |
This comment has been minimized.
This comment has been minimized.
Thanks |
This comment has been minimized.
This comment has been minimized.
this example make thing simpler |
This comment has been minimized.
This comment has been minimized.
great example thanks! |
This comment has been minimized.
This comment has been minimized.
Anyone can tell me how to assign users from the db or active users in to specific Room. Where can we handle that? thanks |
This comment has been minimized.
This comment has been minimized.
Thank you for a great example |
This comment has been minimized.
This comment has been minimized.
Yeah socket.io docs is very bad .For basic knowledge of socket.io tutorial point is best |
This comment has been minimized.
This comment has been minimized.
it's an example. |
This comment has been minimized.
This comment has been minimized.
Many thanks! |
This comment has been minimized.
This comment has been minimized.
Any idea on how to add other users to the room using their socketid. |
This comment has been minimized.
Great example! Thanks!