A lightweight, self-hosted real-time messaging server using the Pub/Sub pattern. Built with Go and WebSockets for low-latency communication.
The server is located in the ./server directory.
cd server
go run main.goROOM.WS uses a simple JSON-based Pub/Sub protocol. It supports rooms, presence (join/leave events), and persistent connections.
ROOMWS_PORT: Port the server listens on (default:8080).ROOMWS_ADMIN_ROOM: Name of the administrative room (default: randomly generated).ROOMWS_ALLOWED_ORIGINS: Comma-separated list of allowed origins (e.g.,room.ws, localhost).localhostand127.0.0.1are always allowed.ROOMWS_WRITE_WAIT: Timeout for writing a message to a client (default:10s).ROOMWS_PONG_WAIT: Timeout for receiving a pong from a client before disconnecting (default:60s). The ping interval is derived as 90% of this value.ROOMWS_RATE_LIMIT: Max new WebSocket connections per second per IP (default:5). Set to0to disable.ROOMWS_RATE_BURST: Burst size for the rate limiter (default:10).ROOMWS_HISTORY_SIZE: Number of messages to retain per room for replay on subscribe (default:0= disabled).ROOMWS_MAX_MESSAGE_SIZE: Max size in bytes of a single incoming WebSocket message (default:32768). Larger messages close the connection.ROOMWS_CLIENT_PUBLISH_RATE: Maxpublishmessages per second per connected client (default:20). Set to0to disable.ROOMWS_CLIENT_PUBLISH_BURST: Burst size for the per-client publish rate limiter (default:40).ROOMWS_TURN_SECRET: Shared secret for minting TURN credentials. Unset (the default) disables the/turnendpoint entirely.ROOMWS_TURN_URLS: Comma-separated TURN URLs handed to clients, e.g.turn:turn.example:3478,turns:turn.example:443?transport=tcp.ROOMWS_TURN_TTL: Lifetime of a minted credential (default:2h).
The server exposes a /health endpoint that returns 200 OK with a JSON body:
{ "status": "ok", "uptime": "5m30s", "clients": 3, "rooms": 1 }The Docker image uses this endpoint as its built-in HEALTHCHECK.
The server exposes a /metrics endpoint in Prometheus text format with gauges for uptime, connected clients, active rooms, goroutines, and allocated memory.
Access requires a bearer token:
curl -H "Authorization: Bearer <token>" https://your-server/metrics
Set the token via ROOMWS_METRICS_TOKEN. If unset, a random token is generated at startup and printed to the log (metrics token configured).
WebRTC applications built on this server can obtain relay credentials from
GET /turn. The endpoint exists only when ROOMWS_TURN_SECRET is set;
otherwise it returns 404, keeping the server a plain pub/sub server.
{
"iceServers": [
{
"urls": ["turn:turn.example:3478", "turns:turn.example:443?transport=tcp"],
"username": "1756118400:a1b2c3d4",
"credential": "base64-encoded HMAC"
}
],
"ttl": 7200
}The credentials follow coturn's REST API convention: the username carries the
expiry, and the password is base64(HMAC-SHA1(secret, username)). Point coturn
at the same secret via use-auth-secret and static-auth-secret, and it will
accept them without any shared user database.
Requests carrying an Origin header are checked against
ROOMWS_ALLOWED_ORIGINS, the same whitelist the WebSocket handshake uses.
See deploy/README.md for a full relay setup behind Caddy.
The client library provides a simple interface to interact with the server.
const drone = new RoomWS('your-channel-id', {
url: 'ws://localhost:8080'
});
drone.on('open', error => {
if (error) return console.error(error);
console.log('Connected with client ID:', drone.clientId);
});const room = drone.subscribe('lobby');
room.on('open', () => {
console.log('Successfully joined lobby');
});
// Receive messages
room.on('message', (message, data) => {
console.log('Received:', message, 'from', data.client_id);
});room.on('members', members => {
console.log('Current members:', members);
});
room.on('member_join', memberId => {
console.log('User joined:', memberId);
});
room.on('member_leave', memberId => {
console.log('User left:', memberId);
});drone.publish({
room: 'lobby',
message: { text: 'Hello World!' }
});By default the server echoes published messages back to the sender (so you receive the server-assigned id and timestamp). Pass no_echo: true to suppress the echo, e.g. when you display your own messages optimistically:
drone.publish({
room: 'lobby',
message: { text: 'Hello World!' },
no_echo: true
});If you are in the admin room, you can send commands as messages:
status: Get server uptime, memory, and client count.add <domain>: Whitelist a new origin.remove <domain>: Remove a domain from the whitelist.list: Show all whitelisted domains.
MIT