socket.io
socket.io copied to clipboard
Cannot read properties of undefined (reading 'protocol')
After setting up the project and running with "npm start" in terminal, the error comes up and saying that protocol is not defined in socket.js. I tried to set up project in a new folder but same thing keeps popping up. \node_modules\socket.io\dist\socket.js:44 if (client.conn.protocol === 3) { ^ TypeError: Cannot read properties of undefined (reading 'protocol')
Hi!
The issue seems to come from this line: https://github.com/socketio/socket.io/blob/134226e96cfacab300ab8f27cf5765d5b07d0271/lib/socket.ts#L165
That being said, I don't think how client.conn
can be undefined there. Which version of socket.io
/Node.js are you using?
I have installed the latest version of socket.io
I have installed the latest version of socket.io
Hi, may be you aren't handle socket middleware like:
io.use((socket, next) => {
const {name, age} = socket.handshake.auth;
if(name) {
socket.name = name;
}
if(age) {
socket.age = age;
}
next()
})
Expected:
io.use((socket, next) => {
const {name, age} = socket.handshake.auth;
if(!name || !age) {
return next();
}
if(age) {
socket.age = age;
return next();
}
socket.name = name;
next()
})
Having same issue after switching to ES Module from commonjs
this fixed it for me
import * as Socket from 'socket.io';
@tulburg I don't think this is related to the issue here, as both CommonJS and ES modules are supported.
I'm going to close this issue, as we were unable to reproduce it.
This code is from the documentation and it is working fine. `import { createServer } from "http"; import { Server } from "socket.io";
const httpServer = createServer(); const io = new Server(httpServer, { /* options */ });
io.on("connection", (socket) => { // ... });
httpServer.listen(3000);`