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
conscious-sapphire•3mo 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
other-emeraldOP•3mo ago
I see, thank you for your insight
frozen-sapphire•3mo 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
stormy-gold•3mo 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. 😢
stormy-gold•3mo 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.
typical-coral•3mo ago
Hey @Joshua where do I add this in the project?
Never mind I scrolled past your comment above the code
typical-coral•3mo ago
@Joshua did that solution work for you? It never successfully connects for me

stormy-gold•3mo 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.sunny-green•2mo ago
This doesn't work with ^1.125 or 1.121.21
rare-sapphire•2mo ago
came up with this, its a hack but whatever its working for me