New Project
Rust WebSocket Server

This example shows how to run a WebSocket server in Rust on Vercel using the
Axum framework. The Vercel Rust runtime
enables HTTP/1.1 upgrades, so axum's WebSocketUpgrade extractor works without
any extra configuration.
api/websocket.rs serves an Axum router behind the VercelLayer:
GET / — an interactive HTML page to connect and exchange messages.GET /ws — the WebSocket endpoint. The handler upgrades the connection and
then runs two concurrent tasks over a single writer:
tick event to the client every second
(server-initiated messages), andA single writer task owns the WebSocket sink and is fed by an mpsc channel, so
both the heartbeat and echo loops can send frames without sharing the
non-clonable sink.
All server messages are JSON tagged with a type field (welcome, echo,
tick), which the front-end renders in the live log.
api/websocket.rs — Axum application with the WebSocket handlerindex.html — interactive WebSocket client (embedded via include_str!)Cargo.toml — Rust dependencies and binary configurationvercel.json — rewrites all routes to the functionClone the repository:
git clone https://github.com/vercel/examples.gitcd examples/rust/websocket
Install Rust if you haven't already:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
Test locally:
vc dev
Then open the printed URL, click Connect, and send a message. You'll see the
welcome event, periodic tick heartbeats, and your echo replies in the log.