Skip to content
coreequipPublic

About

Minimalist Pub/Sub WebSocket server in Go with rooms and presence.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

13 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ROOM.WS Logo

ROOM.WS

A lightweight, self-hosted real-time messaging server using the Pub/Sub pattern. Built with Go and WebSockets for low-latency communication.

Server Setup

The server is located in the ./server directory.

cd server
go run main.go

Protocol

ROOM.WS uses a simple JSON-based Pub/Sub protocol. It supports rooms, presence (join/leave events), and persistent connections.

Environment Variables

  • 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). localhost and 127.0.0.1 are 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 to 0 to 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: Max publish messages per second per connected client (default: 20). Set to 0 to 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 /turn endpoint 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).

Health Check

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.

Metrics

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).

TURN Credentials

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.

Using the Client (roomws.js)

The client library provides a simple interface to interact with the server.

1. Initialize Connection

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);
});

2. Subscribe to a Room

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);
});

3. Presence (Member Events)

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);
});

4. Publish Messages

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
});

Admin Commands

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.

License

MIT

About

Minimalist Pub/Sub WebSocket server in Go with rooms and presence.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Packages

Contributors

Languages