-
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
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
WsAdapter creates two sockets on same server and causes nest to die on ws connection #4968
Comments
Can you provide a minimum reproduction repository? This would help us to fix this issue quicker. Also, if you're interested in creating a PR for this issue, go ahead! PRs are more than welcome :) |
Heyhey, I am not that sure about the structure and idea of the code (esp. why there is the server is encapsulated multiple times), otherwise I would have made a PR. But I created a reproduction repo: https://github.com/lmexo/nest-4968 |
Thanks @ImeXo! I've cloned your repo and everything works properly on my machine:
No crashes |
Okay, that is weird... The problem only occurs when using namespaces 🤔 I updated the reproduction repository accordingly |
Would you like to create a PR for this issue? |
I stumbled upon this issue after googling for the error message. Here's my two cents to this:
|
I wonder if it's possible to use both socket.io and websocket as a gateway in different modules? |
sorry for offtopic @cloakedch why do you think that socket.io-client is going to be deprecated? |
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. |
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). |
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:nest/packages/platform-ws/adapters/ws-adapter.ts
Lines 30 to 51 in 2bdcdb5
The
create
-method is called twice in packages/websockets/socket-server-provider.tscreateSocketServer
callAdapter.create
nest/packages/websockets/socket-server-provider.ts
Line 34 in 2bdcdb5
createWithNamespace
->getServerOfNamespace
-> callsAdapter.create
againnest/packages/websockets/socket-server-provider.ts
Lines 66 to 70 in 2bdcdb5
The
adpter.create()
-method is aware that no server should actually be created but the app's server should be used. Innew 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 andws
throwsEdit: 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
The text was updated successfully, but these errors were encountered: