Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebSocketServer types bug #10210

Closed
5 of 15 tasks
car1ot opened this issue Aug 31, 2022 · 1 comment
Closed
5 of 15 tasks

WebSocketServer types bug #10210

car1ot opened this issue Aug 31, 2022 · 1 comment
Labels
needs triage This issue has not been looked into

Comments

@car1ot
Copy link

car1ot commented Aug 31, 2022

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

I am using socket.io websocket server, following NestJS documentation.
I need to identify users by session token by passing session token to connection url (security reasons).
So I want to check session token in websocket server connection method and disconnect websockets with bad sessions.

There is a method in NestJS Gateway: handleConnection. This method triggers only when socket.io client send specific handshake message!!!. I want make handleConnection to trigger when client just connected without sending specific socket.io protocol messages! There are no way to make this in NestJS.

So I tried to make my own method. And at this point bug appeared.

@WebSocketGateway(3000)
export class AppGateway implements OnGatewayInit {
  constructor() {}

  @WebSocketServer()
  public readonly server: Server;

  afterInit() {
    this.server.engine // => undefined
    this.server.server.engine // => typescript error: "server" value does not exist on type Server
    (this.server as any).server.engine // engine exists!

    (this.server as any).server.engine.on('connection', () => {
      console.log('client enstablished connection!'); // it triggers when client connect!
    })
  }
}

Bug Conclusion: There is no way to access server.engine. It is undefined. Engine accessed by server.server.engine. But there are no type for this.

P.S. about handleConnection method, I think this is new feature request or something like this. It probably not a bug.

Minimum reproduction code

https://codesandbox.io/s/amazing-aj-qoljj4?file=/src/app.gateway.ts

Steps to reproduce

No response

Expected behavior

server.engine should exist on Server type.
At this moment, engine accessed by server.server.engine. But server.server type does not exist.

Package

  • I don't know. Or some 3rd-party package
  • @nestjs/common
  • @nestjs/core
  • @nestjs/microservices
  • @nestjs/platform-express
  • @nestjs/platform-fastify
  • @nestjs/platform-socket.io
  • @nestjs/platform-ws
  • @nestjs/testing
  • @nestjs/websockets
  • Other (see below)

Other package

No response

NestJS version

No response

Packages versions

    "@nestjs/common": "^9.0.2",
    "@nestjs/core": "^8.0.0",
    "@nestjs/platform-socket.io": "^9.0.4",
    "@nestjs/websockets": "^9.0.2",
    "@types/socket.io": "^3.0.2",
    "socket.io": "^4.5.1"

Node.js version

16.14.2

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

@car1ot car1ot added the needs triage This issue has not been looked into label Aug 31, 2022
@kamilmysliwiec
Copy link
Member

Please, use our Discord channel (support) for such questions. We are using GitHub to track bugs, feature requests, and potential improvements.

@nestjs nestjs locked and limited conversation to collaborators Aug 31, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
needs triage This issue has not been looked into
Projects
None yet
Development

No branches or pull requests

2 participants