handleWebSocket
handleWebSocket<
TEnv,TDurableObjectRpc,CFHostMetada>(request,env,_ctx,options):Promise<Response>
Defined in: packages/@livestore/sync-cf/src/cf-worker/worker.ts:142
Handles /websocket endpoint.
Type Parameters
Section titled “Type Parameters”TDurableObjectRpc
Section titled “TDurableObjectRpc”TDurableObjectRpc extends undefined | DurableObjectBranded = undefined
CFHostMetada
Section titled “CFHostMetada”CFHostMetada = unknown
Parameters
Section titled “Parameters”request
Section titled “request”Request<CFHostMetada>
TEnv
ExecutionContext
options
Section titled “options”durableObject?
Section titled “durableObject?”{ name?: ExtractDurableObjectKeys<TEnv>; }
durableObject.name?
Section titled “durableObject.name?”ExtractDurableObjectKeys<TEnv>
Needs to match the binding name from the wrangler config
Default
'WEBSOCKET_SERVER'headers?
Section titled “headers?”HeadersInit
validatePayload?
Section titled “validatePayload?”(payload) => void | Promise<void>
Returns
Section titled “Returns”Promise<Response>
Example
Section titled “Example”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 }) }}Throws
Section titled “Throws”If the payload is invalid