Deploy Bun WebSocket Game Server

Multiplayer WebSocket relay server with rooms and msgpack. Bun-native.

Deploy Bun WebSocket Game Server

Just deployed

Deploy and Host Bun WebSocket Game Server on Railway

Bun WebSocket Game Server is a room-based multiplayer WebSocket relay built on Bun's native WebSocket server. It forwards msgpack-encoded messages between peers in the same room without inspecting payloads, with per-client rate limiting, keepalive, an origin allowlist, and health monitoring. TypeScript, zero framework overhead.

About Hosting Bun WebSocket Game Server

This template deploys a single stateless relay service. Clients connect to wss://<your-domain>/ws/<roomId>, receive a welcome message with their player ID and the current peer list, and every message they send is relayed unchanged to the rest of the room. Because Bun serves WebSockets natively, the server starts in milliseconds and handles thousands of concurrent connections on a small instance. There is no database and no volume to manage. Railway supplies the public domain, automatic SSL, and horizontal scaling. The /health endpoint drives Railway's readiness check and /metrics exposes connection and room counts for monitoring or autoscaling.

Common Use Cases

  • Multiplayer browser games (position sync, input relay, lobbies)
  • Collaborative real-time apps (whiteboards, shared cursors, document editing)
  • Chat rooms where binary msgpack payloads cut bandwidth versus JSON
  • IoT dashboards with low-latency pub/sub between devices and viewers
  • Prototyping networked apps without writing your own server logic

Dependencies for Bun WebSocket Game Server Hosting

  • Bun 1.1+ (native WebSocket server, no extra library)
  • @msgpack/msgpack (binary serialization)
  • TypeScript 5.5+

Implementation Details

Minimal client:

import { encode, decode } from "@msgpack/msgpack";

const ws = new WebSocket("wss://<your-domain>/ws/lobby");
ws.binaryType = "arraybuffer";

let myId;
ws.onmessage = (e) => {
  const msg = decode(new Uint8Array(e.data));
  if (msg.type === "welcome") myId = msg.playerId;
  if (msg.type === "relay") console.log(msg.from, msg.data);
};

ws.onopen = () => ws.send(encode({ type: "position", x: 1, y: 0, z: 2 }));

Configuration is via environment variables: PORT (default 8080), ALLOWED_ORIGINS (comma-separated, default *), KEEPALIVE_MS (idle timeout, default 30000), MAX_MESSAGES_PER_SECOND (default 60), and MAX_PLAYERS_PER_ROOM (default 50).

Why Deploy Bun WebSocket Game Server on Railway?

Railway is a singular platform to deploy your infrastructure stack. Railway will host your infrastructure so you don't have to deal with configuration, while allowing you to vertically and horizontally scale it.

By deploying Bun WebSocket Game Server on Railway, you are one step closer to supporting a complete full-stack application with minimal burden. Host your servers, databases, AI agents, and more on Railway.


Template Content

More templates in this category

View Template
Rocky Linux
Hosted Rocky Linux 9 workspace with SSH and persistent storage. 🚀

codestorm
47
View Template
Foundry Virtual Tabletop
A Self-Hosted & Modern Roleplaying Platform

Lucas
71
View Template
Letta Code Remote
Run a Letta Code agent 24/7. No inbound ports, just deploy.

Letta
51