T
TanStack2mo ago
extended-salmon

Websocket connection to a route

hey guys, need guidance here. Is there an example out there to connect to a route using websocket? here is my attempt:
export const ServerRoute = createServerFileRoute('/ws/session/$id').methods({
GET() {
console.log(':fire: GET method called! This proves the route is being hit')

const event = getEvent()
const req = event.node.req
if (req.headers.upgrade === 'websocket') {
console.log(':rocket: WebSocket upgrade detected! Attempting to handle...')
export const ServerRoute = createServerFileRoute('/ws/session/$id').methods({
GET() {
console.log(':fire: GET method called! This proves the route is being hit')

const event = getEvent()
const req = event.node.req
if (req.headers.upgrade === 'websocket') {
console.log(':rocket: WebSocket upgrade detected! Attempting to handle...')
the issue is, using websocket trying to connect to ws://localhost:3000/ws/session/test-123 even the GET is not called. So there is not even a chance to upgrade to websocket. Help?
8 Replies
other-emerald
other-emerald2mo ago
there is no support for websockets yet really in start. probably needs bigger changes you can leverage nitro though but it's not a start feature
extended-salmon
extended-salmonOP2mo ago
how does that work? do i need to start a separate service?
other-emerald
other-emerald2mo ago
best is to search discord, there were some solutions for websockets floating around right now, the coupling between start and nitro is not optimal w.r.t separations of concerns we are working on this
eastern-cyan
eastern-cyan2mo ago
Correct me if im wrong, but why not using something like partykit, socket.io or pusher for websockets? Unles you really want something skinny those seems to be way more feature complete :p
extended-salmon
extended-salmonOP2mo ago
I searched around and i could not find any solution, there were 2 GitHub links talking about it and one solution that essentially a catch all callback that's not really ideal Does this introduce a new separate service? Because it's important for the socket connection to live in the same process as the web server due to the latency requirement
metropolitan-bronze
metropolitan-bronze2mo ago
in post de-vinxi tss, ive gotten this to work in dev with a vite plugin that works with the vite dev server, but since tss doesnt expose the nitro configuration for production builds, there is currently no clean way to solve this for prod.
extended-salmon
extended-salmonOP2mo ago
@Joshua this is exactly what i end up doing as well, works in dev but not in prod
optimistic-gold
optimistic-gold7d ago
@comfy12345 have you found any ways to get this to work in prod?

Did you find this page helpful?