Skip to content

handleWebSocket

handleWebSocket(request, env, _ctx, options): Promise<Response>

Defined in: packages/@livestore/sync-cf/src/cf-worker/worker.ts:101

Handles /websocket endpoint.

Request

Env

ExecutionContext

{ name?: string; }

string

Needs to match the binding name from the wrangler config

Default

'WEBSOCKET_SERVER'

HeadersInit

(payload) => void | Promise<void>

Promise<Response>

const validatePayload = (payload: Schema.JsonValue | undefined) => {
if (payload?.authToken !== 'insecure-token-change-me') {
throw new Error('Invalid auth token')
}
}
export default {
fetch: async (request, env, ctx) => {
if (request.url.endsWith('/websocket')) {
return handleWebSocket(request, env, ctx, { headers: {}, validatePayload })
}
return new Response('Invalid path', { status: 400, headers: corsHeaders })
}
}

If the payload is invalid