nitrojs / nitro

Next Generation Server Toolkit. Create web servers with everything you need and deploy them wherever you prefer.
https://nitro.build
MIT License
6.16k stars 507 forks source link

Nitro with `bun` preset can't connect to WebSocket during development #2721

Open Anoesj opened 1 month ago

Anoesj commented 1 month ago

Environment


Reproduction

https://github.com/Anoesj/nitro-bun-websocket-issue

[!NOTE]
I could not create a reproduction in Stackblitz/CodeSandbox, as I couldn't figure out how to use Bun there. Therefore, I created a repo instead. Make sure to read the README for reproduction steps.

Describe the bug

I'm trying to get WebSockets to work in a Nuxt application. I've turned on nitro.experimental.websocket in nuxt.config.ts and I'm using the bun Nitro preset.

During development (bun --bun run dev), the WebSocket's onopen event never fires, suggesting the connection can't be fully established, but doesn't "fail" either.

After building the app and previewing it (bun --bun run build && bun --bun run preview), the WebSocket does work.

When using the node-server Nitro preset instead, the WebSocket does not work in both development mode and after building and previewing the app.

When switching to node by omitting the --bun flag (so: bun run dev / bun run build && bun run preview), the WebSocket does work in both development mode and after building and preview the app, for both Nitro preset bun and node-server.

So all in all: Runtime Mode Nitro preset WebSocket works?
Bun Dev bun
Bun Build + preview bun
Bun Dev node-server
Bun Build + preview node-server
Node Dev bun
Node Build + preview bun
Node Dev node-server
Node Build + preview node-server

Additional context

Bun revision: 1.1.27+267afa293 Platform: Linux 6.8.0-40-generic x86_64 x86_64.

If this is a Nuxt issue, feel free to transfer the issue to the Nuxt repository.

Logs

No response

HigherOrderLogic commented 1 month ago

I think it has something to do with crossws, as in the dev and node preset, Nitro is importing the one with Node adapter, not Bun.

rschmied commented 1 month ago

I was commenting on https://github.com/unjs/nitro/discussions/1010 which is pretty similar to what I am running into. The repro there with the latest code (I had to update) and when running with bun did reproduce the error. With some changes to the dev-server/server.ts code I got it to work.