-
Notifications
You must be signed in to change notification settings - Fork 10.1k
Closed
Labels
questionFurther information is requestedFurther information is requested
Milestone
Description
Describe the bug
There seems to be a problem with connection callback on the server if the following occur:
- Server uses dynamic namespaces (e.g.
io.of(/^.*$/)...) - Server sends a message to the namespace before any client connects to that namespace
- When at this point client connects to the server, the
connectioncallback is not triggerred
To Reproduce
Socket.IO server version: 4.5.4
Server
import { Server } from "socket.io";
const io = new Server(3000, {
cors: {
origin: (origin, callback) => callback(null, origin),
},
});
io.of(/^.*$/).on('connection', (socket) => {
console.log('Connected!', socket.id);
});
io.of('testing/namespace').emit('message', 'testing message');Socket.IO client version: 4.5.4
Client
import { io } from "socket.io-client";
const socket = io("ws://localhost:3000/testing/namespace", {});
socket.on("connect", () => {
console.log(`connect ${socket.id}`);
});Expected behavior
The connection callback should be triggered regardless if the first message has been emitted to the namespace before or after any clients connect to that namespace.
Additional context
If we change the server code to the below, it works without any problem.
(first message emitted after first client connects)
const {Server} = require('socket.io');
const io = new Server(3000, {
cors: {
origin: (origin, callback) => callback(null, origin),
},
});
io.of(/^.*$/).on('connection', (socket) => {
console.log('Connected!', socket.id);
io.of('testing/namespace').emit('message', 'test');
});OR (namespace is not dynamic):
const {Server} = require('socket.io');
const io = new Server(3000, {
cors: {
origin: (origin, callback) => callback(null, origin),
},
});
io.of('testing/namespace').on('connection', (socket) => {
console.log('Connected!', socket.id);
});
io.of('testing/namespace').emit('message', 'test');OR (client connecting to another namespace):
import { io } from "socket.io-client";
const socket = io("ws://localhost:3000/some/other/namespace", {});
socket.on("connect", () => {
console.log(`connect ${socket.id}`);
});Metadata
Metadata
Assignees
Labels
questionFurther information is requestedFurther information is requested