Phase 7 · Professional PortfolioModule 51

Capstone: Real-Time Collaboration Service

Create a horizontally scalable collaboration backend with WebSockets, presence, durable events, queues, authorization, and failure recovery.

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.

ConceptWhat it meansDecision rule
PresenceEphemeral liveness with expiryRepresent it as a renewable lease, not durable truth
SequenceMonotonic position in a room event streamUse to detect gaps and order replay
Pub/subBest-effort cross-replica fan-outPair with durable storage when clients must recover missed facts

Professional workflow

Build and verify Node.js programs from the terminal in small, observable steps.

  1. Define the collaboration service boundary: inputs, outputs, invariants, ownership, and expected failures.
  2. Design the data or message contract before choosing implementation details.
  3. Implement the smallest correct path with dependencies passed explicitly.
  4. Add validation, failure translation, cleanup, and concurrency behavior.
  5. Verify the boundary with realistic data and at least one adversarial case.
  6. Measure or observe the behavior before optimizing or extracting abstractions.

Keep the feedback loop short

Run the smallest useful command after every meaningful change. Read the complete error message before editing again, and keep inputs and outputs visible while you learn.

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.

room-protocol.js
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

Treating pub/sub as durable storage makes offline or reconnecting clients silently miss state-changing events.

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

Run the happy path and at least two edge cases, keep responsibilities separated, and add a short README explaining how to run the program.

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