WebSockets passthrough

Implements WebSocket passthrough.

Platform:
Fastly Compute
Language:
Rust
Repo:
https://github.com/fastly/compute-starter-kit-rust-websockets

Use this starter

Using the Fastly CLI, create a new project using this starter somewhere on your computer:

$ fastly compute init --from=https://github.com/fastly/compute-starter-kit-rust-websockets

Or click the button below to create a GitHub repository, provision a Fastly service, and set up continuous deployment:

Deploy to Fastly

Setup

The app expects a configured backend named backend that points to a WebSocket server. For example, if the WebSocket server is available at domain websockets.example.com, then you'll need to create a backend on your Compute service named backend with the destination host set to websockets.example.com and port 443. Also set 'Override Host' to the same host value.

After deploying the app and setting up the backend configuration, all connections received by the service will be passed to the backend.

Enabling WebSockets passthrough

When you create a new service from this starter kit, WebSockets passthrough is enabled automatically.

To enable WebSockets passthrough on an existing Fastly service, type:

fastly products --enable=websockets

Testing locally

If testing locally, make sure that you have a backend named backend defined in the local_server section of your fastly.toml file:

[local_server.backends]
[local_server.backends.backend]
url = "https://websockets.example.com/"
override_host = "websockets.example.com"

Notes

The code in this starter kit cannot be used with the fastly::main attribute on the main() entry point. This is because a function decorated with fastly::main is expected to return a response, but handing off to Fanout is an action that does not create a response. Use an undecorated main() function instead, and use Request::from_client() and Response::send_to_client() as needed.

Compatibility

Next steps

This page is part of a series in the Real-time data use case.

Starters are a good way to bootstrap a project. For more specific use cases, and answers to common problems, try our library of code examples.