nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript šŸš€
https://nestjs.com
MIT License
67.69k stars 7.63k forks source link

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

Closed J-Hoh closed 2 years ago

J-Hoh commented 2 years ago

Is there an existing issue for this?

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?

Other

No response

J-Hoh commented 2 years ago

Made an update to my reproduction repo as im trying to investigate this error. This bug still persists in the latest version, 9.0.5

However, I discovered that setting a different port than the main fastify server, makes the socket.io connection work. Furthermore, I can confirm that setting the port to the same (fastify/NestJS server AND the socket.io gateway) means any ws:// protocol will still get handled by the fastify server. (unwanted)

chadfaurie commented 2 years ago

Hey @J-Hoh,

I am also experiencing this issue, however I found that this is only the case in my local dev environment and not when I have deployed to production, can you confirm if you are experiencing the same?

J-Hoh commented 2 years ago

Just tried it in my reproduction repo above, running npm run build and npm run start:prod (essentially nest build and then running the resulting main.js via node).

For some reason this does not resolve the issue here... Still, using a different port (thus creating a new underlying http server?) does work and is my current workaround

buqiyuan commented 2 years ago

I have the same problem, is there a solution now?

chadfaurie commented 2 years ago

My workaround stopped working in production and I've reverted back to using express for the time being

On Sun, 31 Jul 2022, 06:52 bqy_fe, @.***> wrote:

I have the same problem, is there a solution now?

ā€” Reply to this email directly, view it on GitHub https://github.com/nestjs/nest/issues/9903#issuecomment-1200348130, or unsubscribe https://github.com/notifications/unsubscribe-auth/AM67VIUJIRYDUU6EC3226L3VWYBBXANCNFSM53FLFB2Q . You are receiving this because you commented.Message ID: @.***>

KaMeHb-UA commented 2 years ago

have the same issue both for socket.io and ws

KaMeHb-UA commented 2 years ago

@J-Hoh I was able to launch my app after downgrading @nestjs/platform-fastify to 8.4.7. I can't confirm that everything works fine but at least I've got connected to WS. P.S. using downgraded package you should also add as any right after new FastifyAdapter() in your main.ts file if you're using TypeScript

urdeveloper commented 2 years ago

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.

J-Hoh commented 2 years ago

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

kamilmysliwiec commented 2 years ago

I can't reproduce this issue with the latest versions of deps

andreialecu commented 2 years ago

I had a similar problem. After specifying the host (e.g. await app.listen(3000,'127.0.0.1')) according to fastify/fastify#4053, socket.io started working again. I'm using latest version of NestJs and Fastify.

I just had a similar issue with Mercurius websockets not working at all in certain cases, and this comment helped fix it.

See https://github.com/nestjs/graphql/issues/2365

ahmed-khlifi commented 1 year ago

any update on this, it seems like i can't connect to socket with my client ( mobile app ) with nestJs version 9.*