← Haider Zaman

RUNG: a server-authoritative multiplayer backend in Go

My own project: a four-player Court Piece (Rang) card game with a Kotlin Android client.

RUNG is a real-time card game played in two teams of two. You can try it on playrung.online or Google Play.

In numbers

Figures as of December 2026.

The constraints

Architecture

Android / web client
        |  HTTPS + WebSocket
   rung-server (Go)   REST API, matchmaking, match sessions
        |
   +----+-----+
PostgreSQL   Redis
records      queue, presence, saved tables

Decisions that shaped it

The server decides everything

The client sends an intent such as "play this card". The server owns the shuffle, legality, trick winner, score and rating change. Each seat gets its own snapshot containing only its cards and the number of cards everyone else holds. The rules live in a pure Go package with no clock, database or network, which makes them easy to test.

One goroutine owns one match

A card from a socket, a disconnect, a timeout and a bot's move are all posted to the same channel. The rules engine is never touched concurrently, so a card can't race a dropped connection. The price is that the session's timing logic has to be right, and I learned that the hard way.

A shuffle you can verify

The server draws a random 32-byte seed for each match and publishes a SHA-256 commitment before every hand. The deck is a Fisher-Yates shuffle driven by an HMAC-SHA256 stream, and the seed is revealed when the match ends, so anyone can recompute the deal. I didn't use math/rand because an old match has to produce the same deck after a Go upgrade.

Disconnects are normal

If it's your turn when your connection drops, the turn clock pauses for a limited grace period. After that a bot covers your turns so the other three can keep playing, and only a long absence forfeits the match. Bots see exactly what a human in that seat would see.

Deploys during live games

Live tables sit in memory, so a restart used to end every match. Now each table is saved to Redis whenever it moves. The old server saves, releases ownership and closes sockets with WebSocket code 1012; the app reconnects after a short random delay, and the new server picks the table up. Stopping the old session before releasing it means two servers never drive the same match. Blue/green containers behind nginx keep a warm server ready.

Tradeoffs

Stack

Go, chi, gorilla/websocket, PostgreSQL (pgx), Redis, Firebase, Docker, nginx, Kotlin.

Building something real-time in Go?

Email haiderzamanusafxay@gmail.com and tell me what's going on.