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

Cannot read properties of undefined (reading 'protocol')

Open DAVIGALEE opened this issue 2 years ago • 2 comments

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')

DAVIGALEE avatar Jul 17 '22 08:07 DAVIGALEE

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?

darrachequesne avatar Sep 01 '22 22:09 darrachequesne

I have installed the latest version of socket.io

DAVIGALEE avatar Sep 02 '22 06:09 DAVIGALEE

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()
})

tandv592082 avatar Jan 10 '23 02:01 tandv592082

Having same issue after switching to ES Module from commonjs

this fixed it for me

import * as Socket from 'socket.io';

tulburg avatar Jun 07 '23 16:06 tulburg

@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.

darrachequesne avatar Jun 09 '23 13:06 darrachequesne

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);`

Lokesh396 avatar Jan 09 '24 05:01 Lokesh396