How to use websocket in the latest alpha version?
I saw devinxi is merged, but how to use websocket in the latest alpha version, are there any guides?
10 Replies
fair-rose•6mo ago
As far as I can tell there's no way currently of setting up websockets as nitro config isn't able to be set through the plugin any more
Additionally the auto-generated nitro types no longer include the event handlers so even if you set up a websocket handler though nitro.config.ts it won't have any types or imports
eastern-cyanOP•6mo ago
I see, thank you for your insight
helpful-purple•6mo ago
https://github.com/nick22985/tanstsackstart-socketio
which starts a socket.io server however now trying to figure out how to get that contect inside the serverFn. This only works in dev
metropolitan-bronze•6mo ago
This is bad news, I was hoping that it would become easier to access the Nitro server not harder. Improved WebSocket support was something we were waiting for. 😢
metropolitan-bronze•6mo ago
Here's a simple adapter that should let you create
crossws WebSocket handlers with createServerFileRoute: https://gist.github.com/darkobits/4b2073742af7d89707e216915fae7e9dGist
WebSocket Adapter for TanStack Start
WebSocket Adapter for TanStack Start. GitHub Gist: instantly share code, notes, and snippets.
rival-black•6mo ago
Hey @Joshua where do I add this in the project?
Never mind I scrolled past your comment above the code
rival-black•6mo ago
@Joshua did that solution work for you? It never successfully connects for me

metropolitan-bronze•6mo ago
Yes.
• I'm on the latest version of Start/Router (
^1.121.21)
• The endpoint with the WebSocket handler is at src/routes/api/ws.ts
• I'm able to connect from the client and exchange messages successfully.
Handler:
Test component:
This doesn't require any additional Nitro config (ie: setting experimental.websockets: true). My TanStack Start config (now in vite.config.ts) is completely empty.crude-lavender•5mo ago
This doesn't work with ^1.125 or 1.121.21
deep-jade•5mo ago
came up with this, its a hack but whatever its working for me