Skip to main content
Version: 3.34.0

createWebsocketHandoff

The createWebsocketHandoff() function creates a Response instance which informs Fastly to pass the original Request through Websocket, to the declared backend.

Syntax

createWebsocketHandoff(request, backend)

Parameters

  • request : Request
    • The request to pass through Websocket.
  • backend : string
    • The name of the backend that Websocket should send the request to.
    • The name has to be between 1 and 254 characters inclusive.
    • Throws a TypeError if the value is not valid. I.E. The value is null, undefined, an empty string or a string with more than 254 characters.

Return value

A Response instance is returned, which can then be used via event.respondWith.

Examples

In this example application requests to the path /stream and sent handled via Websocket.

import { createWebsocketHandoff } from "fastly:websocket";

async function handleRequest(event) {
try {
const url = new URL(event.request.url);
if (url.pathname === '/stream') {
return createWebsocketHandoff(event.request, 'websocket_backend');
} else {
return new Response('oopsie, make a request to /stream for some websocket goodies', { status: 404 });
}
} catch (error) {
console.error({error});
return new Response(error.message, {status:500})
}
}

addEventListener("fetch", (event) => event.respondWith(handleRequest(event)));