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.

[9.x.x] Fastify4 Socket.io not exposed

See original GitHub issue

Is 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

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:closed
  • Created a year ago
  • Reactions:1
  • Comments:11 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
J-Hohcommented, Aug 20, 2022

@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.

1reaction
urdevelopercommented, Aug 19, 2022

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Troubleshooting connection issues | Socket.IO
First and foremost, please note that disconnections are common and expected, even on a stable Internet connection:
Read more >
how to not expose server info when loading and connecting to ...
The short answer is : You can not do this. The longer explaination : Your socket.io connection is like any other connection made...
Read more >
socket.io - Best of JS
A heartbeat mechanism is implemented at the Engine.IO level, allowing both the server and the client to know when the other one is...
Read more >
Learn Socket.io In 30 Minutes - YouTube
Socket. io is an amazing library for client/server communication, but it can be a bit ... 267K views 1 year ago Learn X...
Read more >
websocket - Socket.IO Client Security
It's simple and easy but although it's perfectly fine for local testing I am not sure whether is really secure on a live...
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