socket.io icon indicating copy to clipboard operation
socket.io copied to clipboard

Can't send to room

Open jalmarverbraak opened this issue 1 year ago • 0 comments

Describe the bug for some reason socket.io won't send a message to a specific room. right before using the io.to(${socket.id}-devices).emit("event", data) i check what sockets are in said room by using: io.sockets.adapter.rooms.get(${socket.id}-devices) this returns 2 other sockets which is correct, but when I actually emit the event the other sockets don't receive anything. I use the exact same line of code just a different roomname(${socket.sessionId}) which works perfectly fine.

To Reproduce

Please fill the following code example:

Socket.IO server version: 4.8.0

Server

import { Server } from "socket.io";

const io = new Server(3000, {});

io.use((socket, next) => {
  const query = socket.handshake.auth;
  const { type, id, basestation } = query;

  // Store auth data in the socket
  socket.type = type;
  if (type === "basestation") {
    socket.id = id;
    console.log("Basestation connected with ID: ", id);
    socket.join(id);
    socket.join(id + "-devices");
    console.log("Socket joined rooms: ", socket.rooms);
  }

if (type === "device") {
socket.id = id;
socket.join(`${basestation}-devices");
}

  next();
});

io.on("connection", (socket) => {
  console.log(`connect ${socket.id}`);




socket.on("sign", (data) => {

io.to(`${socket.id}-devices`).emit("signRequesttoRoom", data); //is received by only the sending socket
io.to(socket.sessionId).emit("signRequestothertest", data); //is received by the sending socket and the other room sockets
socket.in(`${socket.id}-devices`).emit("signRequestLetsTryThis", data); //is received by none
socket.broadcast.emit("signRequestBR", data);`//is received by all the connected sockets except the sending socket

}

  socket.on("disconnect", () => {
    console.log(`disconnect ${socket.id}`);
  });
});

Socket.IO client version: x.y.z

Client

import { io } from "socket.io-client";

const socket = io("ws://localhost:3000/", {});

socket.on("connect", () => {
  console.log(`connect ${socket.id}`);
});

socket.on("signRequesttoRoom", (data) => {
console.log("signrequesttoRoom", data);
};

socket.on("signRequestothertest", (data) => {
console.log("signRequestothertest", data);
};
socket.on("signRequestLetsTryThis", (data) => {
console.log("signRequestLetsTryThis", data);
};
socket.on("signRequestBR", (data) => {
console.log("signRequestBR, data);
};

socket.on("disconnect", () => {
  console.log("disconnect");
});

Expected behavior I expect the message to be sent to the room which I declared and that the sockets in that room receive it.

Platform:

  • Device: Generic windows laptop and windows desktop
  • OS: Windows/google chrome

jalmarverbraak avatar Oct 17 '24 08:10 jalmarverbraak