Skip to content
All articles

Systems Programming

Synced playback across homes without port forwarding: Rust, Axum and Steam lobbies

You can't ask home users to forward ports, and smart TVs are too weak to resolve and decode video. Here's a Rust server that solves both problems at once while sitting quietly in the system tray.

Fahrettin Baştürk5 min read

When the watch-together mod I wrote for Stardew Valley started working, a new question came up: what about people who don't have the game? Someone joining from the living-room TV or a laptop should be able to sit in the same room. That question led me to write a server, but not a server in the usual sense: a small node that runs in every home and sits quietly in the system tray.

Two realistic constraints

Two constraints shaped the design, and both were about people more than technology.

  1. You can't ask home users to open ports. Logging into the router, setting up forwarding, sharing IP addresses; nobody does that to invite a friend to a movie.
  2. Smart TVs are weak. They have neither the CPU power nor the libraries to resolve video site URLs, parse subtitles or stitch together HLS streams.

So the heavy lifting goes to the server in each home. Between homes, only a tiny bit of information travels: which video, which second, is it playing?

Why Rust?

This server is a program that stays open on someone's computer all day. A memory leak, an orphaned child process or a crash from a race condition translates directly into "this program slows my computer down". Rust's ownership model removes most of those bugs at compile time. With tokio for async I/O and axum, serving both an HTTP API and a WebSocket room on a single port turned out to be quite clean.

let app = Router::new()
    .route("/api/resolve", post(resolve))
    .route("/room", get(room_socket))
    .layer(middleware::from_fn_with_state(state.clone(), require_key))
    .with_state(state);

How does the TV find the server?

Making someone type an IP address with a remote control is one of the worst things you can do. The server answers UDP discovery requests from clients on the same network, so the TV app finds it automatically. If that fails, it scans the local /24 network, and if there are several servers, it lets the user choose. The user never sees this detail, but it completely changes the first five minutes.

One place makes decisions

The most common mistake in sync systems is making decisions in more than one place. In Watch Together there is exactly one authority at any time: if the room's admin is another home, that home; otherwise the connected game; otherwise the server's own clock. Clients follow the room's clock, not their own, so network latency never accumulates into drift.

Connecting homes: Steam lobbies

There are many ways to connect two homes without opening ports; running your own relay server is one. But my users already used Steam, and Steam's lobby infrastructure has already solved bringing two computers behind NAT together. With steamworks I built named, lockable rooms with a per-home limit and friend invites. Steam runs on a separate thread only while a room is open; if nobody is sharing, there's no extra process or network traffic.

There's a nice detail here too: the Stardew mod can launch the server as a hidden helper in --game mode. When the game closes, or even crashes, the stdin pipe closes and the helper shuts itself down. That was the simplest way to join two separate processes without asking the user to set anything up.

Ten clients opening the same video

When five people in a room open the same video at once, resolving the same URL five times is both slow and rude to the video site. Resolver::resolve caches the result and merges concurrent requests into a single flight: the first request starts the resolution, the others wait for the same result.

A stream the TV can handle

I deliberately kept the TV app thin. The server repackages video into fMP4 with ffmpeg so the TV can play it comfortably, and parses subtitles into a list of lines. The TV just plays and follows the room's clock; it finds the current subtitle line with a binary search.

Manners for a program that's always on

  • Shutdown is capped at 3 seconds; no orphaned child processes are left behind.
  • yt-dlp and ffmpeg are downloaded from their official sources with SHA-256 verification on first run.
  • Logs go to dated files, so when something goes wrong there's a place to look instead of asking the user "what happened?".
  • An access-key middleware makes sure not everyone on the same network can send commands to the room.

While writing this server, I kept thinking: good infrastructure is infrastructure the user never notices. Nobody says "wow, UDP discovery works great"; they just turn on the TV and start watching. That was exactly my definition of success.

Fahrettin Baştürk

Software Engineer & Consultant

Share
  • Rust
  • Axum
  • WebSocket
  • Networking

Working on a similar system or problem?Tell me about your project