gornostay25 / svelte-adapter-bun

A SvelteKit adapter for Bun
MIT License
534 stars 33 forks source link

websockets in hook not working #53

Open vyconm opened 7 months ago

vyconm commented 7 months ago

No matter if client-side or server-side, if I open a new websocket, it does not pass through the handle hook. More documentation on the websockets implementation would be helpful, as the bun documentation only partially extends this documentation.

Using the exact code from the example in the readme, in dev, preview and build. e.g.: const ws = new WebSocket( wsURL );

dukejones commented 2 months ago

I ran across the same thing. Diving into the code, there's a hack at the button of /index.js which rewrites handleWebsocket to be this.websocket = -- but I can't find where it's creating the Bun websocket etc.

Try building for production (bun run build, bun run build/index.js) then connect to Websocket on the default port 3000. (has to be /ws by default : ( if (url.pathname.startsWith('/ws')) { )

From other sources on the web, for this to work in development mode we'll need to create a Vite plugin. Not sure how to do that yet.

(Also changing output directory is useful since bun will help since bun will refuse to run the script in package.json if build/index.js exists.)

dukejones commented 2 months ago

I tried like hell to get a self-contained vite plugin working. But finally gave up and used the code in examples/e2e

It uses vite's proxy function, and runs a dedicated Bun process just for websockets.

I think we could change the vite DevServer to use bun for everything. But that's more than I want to bite off right now.

dukejones commented 2 months ago

25 Might be helpful too