Skip to content

WebSocket gateways never see the upgrade request, and lifecycle hooks get the raw uWS socket #205

Description

@nicoske

I hit this while moving a NestJS gateway from socket.io to UwsAdapter on 2.0.1 (also reproduced on current main).

Two related problems in the adapter:

  1. There is no upgrade handler on the uWS route, so the HTTP upgrade request is dropped. The gateway has no way to read the query string or the headers of the connection: auth tokens, user agent, forwarded IPs, none of it is reachable. client.handshake simply doesn't exist anywhere.

  2. handleConnection/handleDisconnect receive the raw uWS socket, while message handlers get the wrapped UwsSocketImpl via @ConnectedSocket. They are different objects with separate data, so anything attached to client.data in handleConnection is invisible to the handlers. It also means the first example in docs/websocket/Lifecycle.md crashes at runtime:

handleConnection(client: UwsSocket) {
  client.emit('welcome', { message: 'Hello!' }); // TypeError: client.emit is not a function
}

The docs already type the hook parameter as UwsSocket, so I think the implementation just never caught up with them.

Repro is small: register a gateway that sets client.data in handleConnection and reads it back in a @SubscribeMessage handler. The handler sees an empty object. Or run the Lifecycle.md example above and watch it throw.

I have a fix ready with e2e coverage (upgrade request captured into socket.handshake with url/parsed query/headers/remote address, and the hooks getting the same wrapped instance the handlers get). PR follows right after this issue. I read the contributing note about waiting for assignment, but since this is a new report with the patch already written I hope it's fine to attach it directly. Happy to rework or split it if you prefer.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions