Regarding the rooms creation in socket.io - node.js

I want to create a room in which rooms should be case insensitive means if room a and room A should open same room or room LOT and room LOt should also same room. So what should i do to make such changes in the room.

Convert all characters of your room name to lowercase (or uppercase) before you connect and before you send messages.
Server side:
io.on('connection', function(socket){
// Join the room
socket.on('join', function(room){
socket.join(room.toLowerCase());
}
// Send message to room
socket.on('message', function(data){
socket.broadcast.to(data.room.toLowerCase()).emit('message', data.message);
}
});
Client side:
socket.emit('join', 'RoOmNaMe');
socket.emit('message', { room: 'rOoMnAmE', message: 'Some message...'});

Related

socket.io not sending to room using socket.to('myroom').emit

I am using socket.io 1.4.0.
I am trying to send a message to put the client in a room called 'myroom', then send a message to the room from the client and then get the response from the room. However I get no response from using
socket.to('myroom').emit('message', 'what is going on, party people?');
Here is my client code:
socket.on('connect', function() {
// Connected, join the booking room for to receive messages for this room
socket.emit('room', 'myroom');
});
socket.on('message', function(msg){
console.log('message: ' + msg);
});
socket.emit('booking room message', {roomid: 'myroom', message: 'hello'});
Here is my server code:
// Set up the Socket.IO server
var io = require("socket.io")(server)
.use(function(socket, next) {
// Wrap the express middleware
sessionMiddleware(socket.request, {}, next);
})
.on("connection", function(socket) {
console.log(socket.request.session);
if (socket.request.session.passport) {
var userId = socket.request.session.passport.user;
//Sign up client for the room from client side.
socket.on('room', function(room) {
socket.join(room);
console.log(userId + ' has joined booking room: ' + room);
});
//make a room for this user
socket.join(userId);
console.log("User ID: " + userId + " connected. socketID: " + socket.id);
socket.on('disconnect', function() {
console.log("User ID: " + userId + " disconnected.");
});
socket.on('booking room message', function(data) {
console.log('message sent from browser to a room');
console.log(data);
socket.to('myroom').emit('message', 'what is going on, party people?');
});
}
});
I get a message back from the server as expected when I use :
socket.emit('message', 'what is going on, party people?');
So there is something wierd going on with the join room function. A couple of things to note, I have some middleware tha gets the ID from express - dont think that is having any impact. Also I create a seperate room for the userID.
Can a user be in two rooms at once? I am not sure what is causing this.
Thanks,
it seemed that neither to or in will send the response back to the same client. The code above did send to other clients in the room however.

Socket.io - method leave doesn't work

I'm creating an online chat on my website.
Almost everything is okay: a client can join a room, he can talk with other clients in the room BUT he can't leave it.
Indeed, when a client try to disconnect a room, it doesn't work.
Here is my server-side code:
io.sockets.on('connection', function (socket) {
// connection
socket.on('room', function(room) {
socket.join(room);
console.log('room join :'+room);
});
// some actions (message, etc)
// disconnection
socket.on('leave', function(room) {
socket.leave(room);
console.log('room quit :'+room);
});
});
I get the "room quit : xxx" in my console but when I try to talk in the room, that still works!
My client can't be disconnected from the room. What goes wrong?

Socketio client identify which room the message came from

If a client joined multiple rooms and receives messages in those rooms is there a way for the client to identify which room his message came from (without incorporating the room in the message)?
When you make a socket join a particular room, you can set property of that socket to that room like this:
io.sockets.on('connection', function (socket) {
var room = "yourRoomName";
socket.on('join room', function(data){
socket.join(room);
socket.room = room;
});
socket.on('message', function(data){
console.log('Received message '+data.message+' from room '+socket.room);
})
});
Client:
socket.emit('join room', {your: 'data'});
socket.emit('message', {message: 'your message'});

socket.io create dynamic room

I have been googling for 2 days on my problem to dynamically create rooms on socket.io using nodeJs. When I create a room to the server, I make it like this:
socket.on('follow_me', function (user_id) { //I use the user_id of the user to create room
socket.join(user_id);
console.log(server.sockets.manager.rooms);
});
If next, I want to send a message to all persons connected in the by using
socket.join(user_id);
when i use :
socket.on('message', function (data) {
socket.broadcast.to(data.room).emit('recieve',data.message); //emit to 'room' except this socket
});
The other users in this room do not receive the message; but if I use:
socket.join(user_id);`,when i use :
socket.on('message', function (data) {
socket.broadcast.emit('recieve',data.message);
});
all user receive the message. Why room do not work for me? I think the code is good!
Tank's
socket.on('message', function (data) {
/*considering data.room is the correct room name you want to send message to */
io.sockets.in(data.room).emit('recieve', data.message) //will send event to everybody in the room while
socket.broadcast.to(data.room).emit('recieve', data.message) //will broadcast to all sockets in the given room, except to the socket which called it
/* Also socket.broadcast.emit will send to every connected socket except to socket which called it */
});
I guess what you need is io.sockets.in

Node.js Socket.IO Best way to listen room connection?

My bad way
Client-Side
socket.on('connect',function(){ socket.emit('EnterRoom',{room:'test'});})
Server-Side
app.js
io.sockets.on('connection',function(socket){
socket.on('EnterRoom', function(data) {
socket.join(data.room);
app.emit('event:JoinRoom',{room:data.room});
})});
some-module-with-rooms.js
app.on('event:JoinRoom',function(data){if(data.room=='test'){/*anycode*/} });
From the Wiki about Rooms:
Emitting an event to all clients in a particular room:
io.sockets.in('room').emit('event_name', data)
So in your case:
io.sockets.on('connection',function(socket){
socket.on('EnterRoom', function(data) {
socket.join(data.room)
io.sockets.in(data.room).emit('JoinRoom', data);
})
});
That will emit only to the selected room.
You can the access the room:
io.sockets.manager.rooms
and i quote the docs:
This is a hash, with the room name as a key to an array of socket IDs. Note that the room names will have a leading / character

Resources