createWebsocketHandoff

Functionin websocket@fastly/js-compute 4.0.0since 3.34.0View source
createWebsocketHandoff(request: Request, backend: string): Response

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 Error if request is not a Request instance, or if backend is empty or longer than 254 characters
1
2
3
4
5
6
7
8
9
10
11
import { 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)));