Newsletter
Join the Community
Subscribe to our newsletter for the latest news and updates
Run a WebSocket server in Rust on Vercel with Axum, featuring real-time echo and heartbeat messages.
Rust WebSocket is a starter template from Vercel that demonstrates how to deploy a WebSocket server written in Rust on the Vercel platform using the Axum web framework. It is a minimal example that covers the full setup: a WebSocket endpoint, a client page, and the configuration needed to run on Vercel's Rust runtime.
This template is a minimal Axum-based WebSocket server designed to run on Vercel's Rust runtime. It takes incoming HTTP connections at the /ws endpoint, upgrades them to WebSocket, and then simultaneously pushes a tick heartbeat every second and echoes back any message the client sends. The server is published by Vercel and is part of the vercel/examples repository, which contains other Rust templates like rust-axum and rust-hello-world.
WebSocketUpgrade extractor works without extra configuration.index.html provides a live log that renders all server messages tagged with a type field (welcome, echo, tick).api/websocket.rs, with Cargo.toml for dependencies and vercel.json rewriting all routes to the function.include_str!, keeping the deployment to a single function file.welcome, tick, and echo events in real time.The api/websocket.rs file defines an Axum router with two routes: GET / serves the interactive HTML client, and GET /ws handles the WebSocket upgrade. After upgrade, the handler spawns two concurrent tasks — a heartbeat that sends a tick message every second and an echo loop that mirrors client messages — both feeding into a single writer task through an mpsc channel. To run locally, install Rust, clone the repository, and run vc dev.
Yes. The template leverages Vercel's Rust runtime for serverless functions, which supports HTTP/1.1 upgrades. This allows Axum's WebSocketUpgrade to function without any extra configuration on the platform.
The server sends three types of JSON messages: welcome when a client connects, tick every second from the heartbeat, and echo that mirrors any text the client sends. All messages include a type field for easy handling on the client side.
Clone the vercel/examples repository, navigate to the rust/websocket directory, install Rust, and run vc dev (requires Vercel CLI). Open the printed URL, click Connect, and send messages to see the log.
The page does not specify a minimum Rust version, but it requires standard tools like Cargo and rustup. The dependencies are defined in Cargo.toml, so building the project resolves them automatically.
