question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

WsAdapter creates two sockets on same server and causes nest to die on ws connection

See original GitHub issue

Bug Report

Current behavior

Creating a new nest instance with ws websocket adapter, nest creates two sockets on the same server. Using nest’s default server booted in main.ts is common getting a websocket connection through this server by upgrading the connection. As I understood the docs, this should be accomplished by just not specifying a port number with the @WebSocketGateway() decorator.

Expected behavior

Allow me to open my socket without crashing nest 😃

Possible Solution

The ws-Adapter runs new websocket.Server(...) twice on the same httpServer in line 37 below: https://github.com/nestjs/nest/blob/2bdcdb5814db144082b0e4107f7b02f9d2d7a720/packages/platform-ws/adapters/ws-adapter.ts#L30-L51

The create-method is called twice in packages/websockets/socket-server-provider.ts

The adpter.create()-method is aware that no server should actually be created but the app’s server should be used. In new ws.Server() the socket-server registers for the upgrade-event on the underlaying httpServer. Since two Socket’s are created on this server, both registered upgrade-handlers are fired from the same server-instance and ws throws

Error: server.handleUpgrade() was called more than once with the same socket, possibly due to a misconfiguration
    at WebSocketServer.completeUpgrade (node_modules\ws\lib\websocket-server.js:267:13)
    at WebSocketServer.handleUpgrade (node_modules\ws\lib\websocket-server.js:245:10)
    at Server.upgrade (node_modules\ws\lib\websocket-server.js:89:16)
    at Server.emit (events.js:201:15)
    at onParserExecuteCommon (_http_server.js:580:14)
    at onParserExecute (_http_server.js:520:3)

Edit: The upgrade-handlers are called when a WebSocket trys to connect, not on application startup - and then nest crashes.

Although the error comes from ws it is due to wrong usage by nest.

Environment


Nest version: 7.4.1



Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
cloakedchcommented, Feb 3, 2021

There were numerous hints from Damien Arrachequesne in the issue tracker that plain ws is the way to go. Today, it may look different again.

0reactions
kamilmysliwieccommented, Feb 3, 2021

In the next major release (v8), this will simply crash during the application bootstrap process, warning you that “namespaces” are no supported by the “ws” package (it’s an exclusive socket.io feature).

Read more comments on GitHub >

github_iconTop Results From Across the Web

How WebSocket server handles multiple incoming connection ...
Client A connects to server on port 80 with HTTP request to initiate a webSocket connection. This process creates a socket between the...
Read more >
Adapter - Gateways | NestJS - A progressive Node.js framework
create, Creates a socket instance based on passed arguments ... However, to somehow mimic this feature, you can mount multiple ws servers on...
Read more >
webSocket - RxJS
When WebSocketSubject is subscribed, it attempts to make a socket connection, unless there is one made already. This means that many subscribers will...
Read more >
Troubleshooting connection issues | Socket.IO
the server itself may be killed as part of an autoscaling policy; the user may lose connection or switch from WiFi to 4G,...
Read more >
Comparing WebSockets and Server-Sent Events | Ably Blog
Summary of key WebSockets features · Uses a custom ws protocol to transport messages, which works at a lower level than HTTP. ·...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found