[9.x.x] Fastify4 Socket.io not exposed
See original GitHub issueIs there an existing issue for this?
- I have searched the existing issues
Current behavior
Using NestJS@9 with the fastify-adapter and socket.io, WebsocketGateways initiate and everything else serverside seems to work, but I cannot get any clients to connect anymore. This problem did not exist on NestJS@8
On a bigger repo i get a 404 error for every trying socket.io client connection:
NestJS encountered an Error:
NotFoundException: Cannot GET /ws/?EIO=4&transport=websocket
at callback (C:\project\node_modules\.pnpm\@nestjs+core@9.0.1_k4wxejbqff45bi5i3zl7of6cpm\node_modules\@nestjs\core\router\routes-resolver.js:77:19)
at Object.<anonymous> (C:\project\node_modules\.pnpm\@nestjs+core@9.0.1_k4wxejbqff45bi5i3zl7of6cpm\node_modules\@nestjs\core\router\router-proxy.js:9:23)
at preHandlerCallback (C:\project\node_modules\.pnpm\fastify@4.2.0\node_modules\fastify\lib\handleRequest.js:126:28)
at preValidationCallback (C:\project\node_modules\.pnpm\fastify@4.2.0\node_modules\fastify\lib\handleRequest.js:110:5)
at handler (C:\project\node_modules\.pnpm\fastify@4.2.0\node_modules\fastify\lib\handleRequest.js:74:7)
at handleRequest (C:\project\node_modules\.pnpm\fastify@4.2.0\node_modules\fastify\lib\handleRequest.js:22:5)
at runPreParsing (C:\project\node_modules\.pnpm\fastify@4.2.0\node_modules\fastify\lib\route.js:487:5)
at next (C:\project\node_modules\.pnpm\fastify@4.2.0\node_modules\fastify\lib\hooks.js:168:7)
at C:\project\node_modules\.pnpm\@fastify+cors@8.0.0\node_modules\@fastify\cors\index.js:127:12
at resolveOriginOption (C:\project\node_modules\.pnpm\@fastify+cors@8.0.0\node_modules\@fastify\cors\index.js:85:129) {
response: {
statusCode: 404,
message: 'Cannot GET /ws/?EIO=4&transport=websocket',
error: 'Not Found'
},
status: 404
}
From the error i gather that the request gets passed through the fastify instance, getting checked by its cors module (for which I’ve tried and set origin to “*”) but it seems to be searching for an http get route, instead of the websocket upgrade way.
The minimum reproduction code below has 4 branches, NestJS@8/9 paired with fastify@3/4 respectively, and express@4. The issue is not present when using express. The issue is also not present with NestJS@8 and thus fastify@3.
It does not seem to me like this issue is coming from fastify@4, as this repos example uses fastify@4 without a problem.
Minimum reproduction code
https://github.com/J-Hoh/nestjs9_fastify4_socketio-bug/tree/nest9_express4
Steps to reproduce
No response
Expected behavior
Client websockets to connect / The server instance handling the upgrade request properly
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
9.0.1
Packages versions
[System Information]
OS Version : Windows 10
NodeJS Version : v16.15.1
PNPM Version : 7.3.0
[Nest CLI]
Nest CLI Version : 9.0.0
[Nest Platform Information]
platform-socket.io version : 9.0.1
platform-fastify version : 9.0.1
websockets version : 9.0.1
schematics version : 9.0.1
common version : 9.0.1
core version : 9.0.1
cli version : 9.0.0
Node.js version
16.15.1
In which operating systems have you tested?
- macOS
- Windows
- Linux
Other
No response
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:11 (2 by maintainers)
@KaMeHb-UA @urdeveloper I’m pretty sure the problem was introduced with the upgrade to 9.0.0 of NestJS packages, and the problem is with having a websocket server on the same host+port as the actual fastify http server.
Using a different port or even just specifying a host will create a separate server. Using the same port or not specifying a host means the http fastify server will be used to register the websocket server, which seems to not work after 9.0.0? The issue isnt big enough for me to try and investigate further, and the workaround of using a separate port is fairly simple.
I had a similar problem. After specifying the host (e.g.
await app.listen(3000,'127.0.0.1')
) according to https://github.com/fastify/fastify/issues/4053, socket.io started working again. I’m using latest version of NestJs and Fastify.