What you'll learn
Build a horizontally scalable collaboration backend with authenticated WebSockets, room authorization, presence leases, durable domain events, pub/sub fan-out, reconnect recovery, backpressure, and resilience evidence.
By the end of this lesson, you'll be able to:
- Specify a collaboration protocol
- Separate presence from durable state
- Scale fan-out across replicas
- Recover ordering and missed events
Core mental model
Node.js becomes easier when you separate the JavaScript language from the runtime and the operating-system capabilities it exposes. Use this table as a decision guide.
| Concept | What it means | Decision rule |
|---|---|---|
| Presence | Ephemeral liveness with expiry | Represent it as a renewable lease, not durable truth |
| Sequence | Monotonic position in a room event stream | Use to detect gaps and order replay |
| Pub/sub | Best-effort cross-replica fan-out | Pair with durable storage when clients must recover missed facts |
Professional workflow
Build and verify Node.js programs from the terminal in small, observable steps.
- Define the collaboration service boundary: inputs, outputs, invariants, ownership, and expected failures.
- Design the data or message contract before choosing implementation details.
- Implement the smallest correct path with dependencies passed explicitly.
- Add validation, failure translation, cleanup, and concurrency behavior.
- Verify the boundary with realistic data and at least one adversarial case.
- Measure or observe the behavior before optimizing or extracting abstractions.
Keep the feedback loop short
Guided code lab
Recover from an event gap
The sequence number lets a client detect missing frames and request durable events before resuming live delivery.
socket.on('room.event', async (event) => {
const expected = roomState.lastSequence + 1;
if (event.sequence !== expected) {
const missed = await api.get('/rooms/' + event.roomId + '/events?after=' + roomState.lastSequence);
for (const durableEvent of missed.items) apply(durableEvent);
} else {
apply(event);
}
});Production practice
Contract
The protocol defines identity, room membership, command/event schemas, sequence and deduplication, presence lease, replay window, limits, and error recovery.
Verification
Test unauthorized rooms, duplicate and out-of-order events, dropped pub/sub messages, reconnect gaps, replica loss, hot rooms, and slow consumers.
Operations
Observe connections, presence churn, room fan-out, publish lag, replay size, buffered bytes, authorization denials, and regional failure assumptions.
Common failure mode
Independent workshop
Build and demonstrate a production-shaped real-time collaboration backend.
Your finished workshop must include:
- Protocol document
- Authenticated gateway
- Presence leases
- Durable sequenced events
- Cross-replica pub/sub
- Chaos/load report
Definition of done
Recap & quick check
Key takeaways
- Presence is ephemeral
- State-changing events are durable
- Sequences reveal gaps
- Pub/sub handles live fan-out
- Reconnect is a core workflow
Quick check
1. How should presence expire?
2. What reveals a missed event?
3. What is pub/sub alone missing?
Next: System Design & Portfolio Review