Relay
(Real-time Community Chat)A self-owned community chat app with channels, threads and presence, built for reliable delivery and recoverable connections.
- 100 virtual users in local load test
- 1,800 messages persisted in local test
- 0 failed sends in local test
2026 · React · TypeScript · Node.js · WebSockets · PostgreSQL · Redis
Problem
(what hurt)
Real-time chat needs to survive lost acknowledgements, out-of-order events and interrupted connections without duplicating messages or silently losing history.
Project goal: Build a self-owned space for public and private communities, with channel conversations, threaded replies and live presence that recover when connections drop.
Architecture
(how it fits together)
A React client connects to Node.js HTTP and WebSocket handlers. Each send checks membership and locks the room row in a PostgreSQL transaction, assigns a sequence number and commits before publishing through Redis Pub/Sub. Clients reconcile optimistic messages and fetch missing sequences over REST.
Tech decisions: React, Vite and Zustand power a virtualized, optimistic chat UI. A Node.js WebSocket validates shared Zod events, Drizzle and PostgreSQL persist messages, and Redis Pub/Sub distributes live events.
- React
- TypeScript
- Node.js
- WebSockets
- PostgreSQL
- Redis
// Persist first; retries reuse the original clientId.
result = await sendMessage(db, {
userId: conn.user.id, roomId: ev.roomId,
clientId: ev.clientId, body: ev.body,
parentId: ev.parentId ?? null,
});
const event: BusEvent = {
type: "message.new", message: result.message,
committedAt: Date.now(),
};
const topic = topics.room(ev.roomId);
const published = await publishWithRetry(this.relay.bus, topic, event);Features
(what it does)
Channels, threads and live presence
Community-scoped channels support one-level threaded replies, typing indicators and multi-tab presence. Virtualized history and optimistic sending keep conversations responsive.
Public and private communities
Members discover public communities; private communities use email-bound, single-use invitations that expire after seven days. Owners manage members and channels, while superadmins manage communities and accounts.
Recovery built into messaging
Pending sends retain their clientId across retries. Server sequence numbers order messages, REST fills gaps, and offline messages queue until the browser reconnects.
Challenges
(what was hard)
01Retries without duplicate storage
A room-row lock serializes sequence allocation, and a unique author + clientId constraint makes retries return the original message. Live events may be duplicated or missed during failures; clients de-duplicate by ID and recover missing messages from PostgreSQL through REST history sync.
02Connections with a fixed lifetime
Planned reconnects open a new socket before retiring the old one. Jittered retries, history sync and expiring presence help unexpected drops recover without confusing multi-tab status.
03Access control across live connections
Community and channel permissions are checked for history, subscriptions, sends, typing and live delivery. Membership changes propagate between instances, and superadmin status does not grant access to private community conversations.
Results
- 100 virtual users in local load test
- 1,800 messages persisted in local test
- 0 failed sends in local test
Relay brings community management and recoverable chat into one app. In a recorded local test, 100 virtual users sent 1,800 messages across two instances with zero failed sends. Receive latency was 26 ms median / 409 ms p95 on a shared single-core machine.
design & code by Shreya Pathak