Is there support for SSE and/or web sockets?
Is there support for SSE and/or web sockets? If not is it planned?
7 Replies
ambitious-aqua•5mo ago
Hey! Quick answer is yes, via Nitro, which is used internally by TS Start. I had a thread opened about this as well: https://discord.com/channels/719702312431386674/1354142236433973269/1354142236433973269
https://nitro.build/guide/websocket
WebSocket - Nitro
Nitro natively supports a cross platform WebSocket API
ambitious-aqua•5mo ago
There's an example in the docs: https://tanstack.com/start/latest/docs/framework/react/server-functions#returning-raw-response-objects
2 things to keep in mind though:
- The example uses a server function, but you need a fixed URL, so I'd suggest that you use an API route instead
- It works well on dev, but you may run into issues when deploying, depending on the hosting provider you use (I'm currently having issues with CloudFlare on my end)
Server Functions | TanStack Start React Docs
What are Server Functions? Server functions allow you to specify logic that can be invoked almost anywhere (even the client), but run only on the server. In fact, they are not so different from an API...
eager-peach•4mo ago
I found the approach in this post useful: https://nize.ph/blog/tanstack-start-websockets/
It uses H3 and crossws to give you a really nice event-based API for handling WebSockets. This will break when they drop Vinxi, but hopefully accessing more of Nitro's features like its great WebSockets support will be made easier.
absent-sapphireOP•4mo ago
Hey, thanks to you both 🙏
eager-peach•3mo ago
Note: This method no longer works with more recent versions that have migrated off Vinxi. Sadly, it now appears more difficult to access the Nitro configuration to enable things like WebSockets.
wise-white•3mo ago
when this is out it should be possible again and more flexible
https://discord.com/channels/719702312431386674/1382130510402162698
like-gold•2mo ago
Heya, it's been a while since this post, are there any changes or we should use separated backend for this need(sockets)?