Skip to content

Instantly share code, notes, and snippets.

@darkobits
Last active January 28, 2026 05:59
Show Gist options
  • Select an option

  • Save darkobits/4b2073742af7d89707e216915fae7e9d to your computer and use it in GitHub Desktop.

Select an option

Save darkobits/4b2073742af7d89707e216915fae7e9d to your computer and use it in GitHub Desktop.
WebSocket Adapter for TanStack Start
import { json } from '@tanstack/react-start'
import { getEvent, type ServerRouteMethodsRecord } from '@tanstack/react-start/server'
import createNodeAdapter, { type NodeOptions as NodeAdapterOptions } from 'crossws/adapters/node'
/**
* Adapter for TanStack Start and `crossws`.
*
* @see https://tanstack.com/start/latest/docs/framework/react/server-routes
* @see https://nitro.build/guide/websocket
*
* @example
*
* ```
* import { createServerFileRoute } from '@tanstack/react-start/server'
* import { createWebSocketHandler } from '...'
*
* export const ServerRoute = createServerFileRoute('/api/ws').methods(createWebSocketHandler({
* serverOptions: {
* // ...
* },
* hooks: {
* open: () => {
* // ...
* },
* message: () => {
* // ...
* },
* error: : () => {
* // ...
* },
* close: : () => {
* // ...
* },
* }
* }))
* ```
*/
export function createWebSocketHandler(options: NodeAdapterOptions): ServerRouteMethodsRecord<any, any, any> {
const nodeAdapter = createNodeAdapter(options)
const handler = async () => {
try {
const event = getEvent()
if (event.node.req.headers.upgrade === 'websocket') {
const { req } = event.node
const { socket } = req
if (socket) {
await nodeAdapter.handleUpgrade(req, socket, Buffer.from([]))
return
}
}
return json({ message: 'This is a WebSocket endpoint.' }, { status: 400, statusText: 'Bad Request' })
} catch (error: any) {
const message = error.message ?? 'An unknown error occurred.'
return json({ message }, { status: 500, statusText: 'Internal Server Error' })
}
}
return {
GET: handler,
POST: handler
}
}
@devpanda0
Copy link

Hey, which version of @tanstack/react-start are you using for that? In version 1.132.31, there is no getEvent and no ServerRouteMethodsRecord.

@4lisastra-Pur
Copy link

4lisastra-Pur commented Jan 28, 2026

on this https://www.answeroverflow.com/m/1382191056187035658
the owner say

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.

and other user say
This doesn't work with ^1.125 or 1.121.21

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment