Websocket support in tanstack start #4576
Replies: 8 comments 5 replies
-
|
Same question here. I've just spent some time trying to get this to work and couldn't get to. I cannot find much around websockets on the documentation of any part of the stack either. |
Beta Was this translation helpful? Give feedback.
-
|
https://discord.com/channels/719702312431386674/1382130510402162698 probably this when it's out. this should make implementing websockets even more flexible. i'll get around to updating my guide when I have the time |
Beta Was this translation helpful? Give feedback.
-
|
how do you implement webscoket in a tanstack start app right now? |
Beta Was this translation helpful? Give feedback.
-
|
For Cloudflare users, I just published a new guide that uses Durable Objects: https://nize.ph/tanstack-start-websockets-cloudflare/ Demo: https://ts-cf-websockets.mugnavo.com/ |
Beta Was this translation helpful? Give feedback.
-
|
Hi, It is possible to rely on the default Nitro server, albeit in a rather "inelegant" fashion. In your {
// ...,
plugins: [
devtools(),
nitro({
// ...,
serverDir: "<serverDir>",
features: {
websocket: true,
}
}),
// ...
]
}In the import { defineWebSocketHandler } from "nitro/h3";
export default defineWebSocketHandler({
open(peer) {
peer.send({ id: peer.id, message: "server hello" });
peer.publish("channel", { id: peer.id, status: "joined" });
peer.subscribe("channel");
},
message(peer, message) {
if (message.text().includes("ping")) {
peer.send({ id: "server", status: "pong" });
}
},
close(peer) {
peer.publish("channel", { id: peer.id, status: "left" });
},
});With that you will have fully functional WebSockets, directly working alongside your TanStack Start routes. new WebSocket("ws://<address>/message");Because the existing Nitro presets are not exposing the WebSocket server instance you will want to create your own, or be creative with your communication bus. (i.e. extract the peers list from the peer pointer in the event handler hooks. I hope this helps while we wait for an more supported solution. Best to all ! |
Beta Was this translation helpful? Give feedback.
-
|
You explained it perfectly, I use it exactly the same way! |
Beta Was this translation helpful? Give feedback.
-
|
What is the current best practice for adding WebSocket support to TanStack Start (v1.163)? I've been researching this thoroughly and hit several dead ends. Here's what I found: What no longer works
How TanStack Start uses srvx (the core issue)After digging into the source, I found that TanStack Start doesn't actually expose srvx's server configuration:
So even if srvx adds WebSocket support in a future version, TanStack Start's dev/preview modes would need to be updated to pass through those options. My use caseI need a persistent bidirectional connection for real-time chat/notifications on the same host/port as my TanStack Start app. For now I'm planning to use SSE ( Questions
Environment: TanStack Start v1.163.2, srvx 0.11.8, h3 2.0.1-rc.14. Thanks! |
Beta Was this translation helpful? Give feedback.
-
|
I had to use websockets for my usecase so after bashing my brains for a long time, I found a workaround.
"scripts": {
"dev": "VITE_USE_NITRO=true vite dev",
nitro({ serverDir: false, features: {websocket: true}}),
import { createFileRoute } from "@tanstack/react-router";
import { defineHooks } from "crossws";
const hooks = defineHooks({
open: console.log
});
export const Route = createFileRoute("/api/machine/")({
server: {
handlers: {
GET: async () => {
return Object.assign(
new Response("WebSocket upgrade is required.", {
status: 426,
}),
{
crossws: hooks,
},
);
},
},
},
}); |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
What’s the best approach to integrate websocket support in tanstack start that aligns with future direction given the recent changes?
https://nize.ph/posts/tanstack-start-websockets/
Beta Was this translation helpful? Give feedback.
All reactions