Socket.io - method leave doesn't work - node.js

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?

Related

socket.io does not close previous connection

we have this socket.io serverside that on auth joins the user in the rooms he is in, but on disconnect it seems like it does not really disconnect, because on a new connect i get 2 connections, seen in screenshot. Also included is the disconnect code part.
connection part:
io.on("connection", function(socket) {
console.log("connected");
console.log("user connected with " + socket.id);
disconnect part:
socket.on("disconnect", function() {
socket.removeAllListeners();
if (socket.auth) {
socket.groups.forEach(group => {
console.log("left " + group.chatGroupID);
socket.leave(group.chatGroupID);
});
}
delete socket[socket.id];
socket.disconnect(true);
console.log(socket.disconnected);
console.info("disconnected user (id=" + socket.id + ").");
});
server printout:
somehow i globbed all files in my folder sockets and thus nested my sockets which caused this weird behavior

Regarding the rooms creation in socket.io

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...'});

Why does socket.io not join a given room?

I'm having problems when I try to join a socket to a room. The name of the room is the socket.id and it is assigned when it is connected. But when this happens it doesn't join to room. What could be the error?
Client code:
var socket = io();
socket.on("connect", function() {
socket.emit("joinRoom", socket.id);
});
socket.on("agentMessage", function(msg) {
$('#messages').append("<div class='agent-messages'><p>" + msg + "</p></div>");
});
Server code:
io.on("connection", (socket) => {
let roomID;
socket.on("joinRoom", (socket_id) => {
roomID = socket_id;
socket.join(roomID);
socket.to(roomID).emit("agentMessage", "Welcome!");
});
socket.on("disconnect", () => {
socket.leave(socket_id);
});
});
Your client is actually joining the room, but it is not sending the message. As you are sending message from the server side you cannot send it using the socket, as socket represents a client connection and cannot throw events on behalf of server. You have to use
io.to(roomID).emit("agentMessage", "Welcome!");
as io represents a server instance and can throw events on behalf of server(actually it is the server).
Reference

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.

can i give my own id to a connected socket in nodejs

am trying to implement a multiuser communication app. I want to identify a user's socket with his id, so can i set socket's id to user' id like
socket.id=user.userId;
This may help you
io.sockets.on('connection', function (socket) {
console.log("==== socket ===");
console.log(socket.id);
socket.broadcast.emit('updatedid', socket.id);
});
you can save socket id in client side. When you want 1-1 message (private chat) use updated socket id. Some thing like this :
io.sockets.socket(id).emit('private message', msg, mysocket.id)
You can do this, but use property that doesn't clash with Node.js or any other frameworks keys
socket.myappsuperuserid = user.userId;
Here
client side
var socket = io.connect();
socket.on('connect', function () {
console.log("client connection done.....");
socket.emit('setUserId','random value');
});
On server side
io.sockets.on('connection', function (socket) {
socket.on('setUserId',function(uId){
socket.userId = uId;
});
});
This may help...
In socket.io when a user connects, socket.io will generate a unique socket.id which basically is a random unique unguessable id. What i'd do is after i connect i call socket.join('userId'). basically here i assign a room to this socket with my userId. Then whenever i want to send something to this user, i'd do like this io.to(userId).emit('my message', 'hey there?').
Hope this help.

Resources