createWebsocketHandoff
Create a Response that instructs Fastly to pass the original request through as a WebSocket connection to the specified backend.
Note: Can only be used when processing requests, not during build-time initialization.
Parameters
- request: Request
The request to pass through as a WebSocket connection
- backend: string
The name of the backend to send the request to (1–254 characters)
Returns
Throws
- Throws an
Errorifrequestis not a Request instance, or ifbackendis empty or longer than 254 characters
1234567891011import { createWebsocketHandoff } from "fastly:websocket";
async function handleRequest(event) { const url = new URL(event.request.url); if (url.pathname === '/stream') { return createWebsocketHandoff(event.request, 'websocket-backend'); } return new Response('Not found', { status: 404 });}
addEventListener("fetch", (event) => event.respondWith(handleRequest(event)));