---
hide_title: false
hide_table_of_contents: false
pagination_next: null
pagination_prev: null
---

# createWebsocketHandoff

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

## Syntax

```js
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`](../globals/TypeError/TypeError.mdx) 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.

```js
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)));
```
