Replies: 3 comments 2 replies
-
Hi! A classic cookie should do the trick: import { Server } from "socket.io";
import { serialize } from "cookie";
const io = new Server(httpServer, {
allowRequest: async (req, callback) => {
// parse the session from the request headers
const session = await fetchSession(req);
req.session = session;
callback(null, true);
}
});
// send the session cookie during the handshake
io.engine.on("initial_headers", (headers, req) => {
headers["set-cookie"] = serialize("sid", req.session.id, { sameSite: "strict" });
});
io.use(async (socket, next) => {
const sessionId = socket.request.session.id;
const sockets = await io.in(sessionId).fetchSockets();
if (sockets.length) {
// another socket with this session ID already exist
next(new Error("unauthorized"));
} else {
socket.join(sessionId);
next();
}
}); Reference: |
Beta Was this translation helpful? Give feedback.
-
It works fine when I replace this: by
I get an error with fetchSession, it says it is not defined, what exactly should it do here? |
Beta Was this translation helpful? Give feedback.
-
Hi |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi
Let's imagine I have 2 tabs open on iPhone with 2 times the same application on in each tab (it's already happened by accident it's like clones), and there is a big conflict because the two tabs connect everything both to socket.io server, I would like to deny socket.io access to the second tab if the first one is already connected to the socket.io.
Does anyone know how to detect the same and unique browser (from a single client) with several tabs on the same socket.io application so that it does not connect to it?
Cdt
Beta Was this translation helpful? Give feedback.
All reactions