Script Valley
WebSockets & Real-Time Applications
Real-Time Patterns and Architecture/Assessment

Practice & Assessment

Test your understanding of Real-Time Patterns and Architecture

Multiple Choice Questions

6
1

What must you do when an optimistic UI update receives a failure response from the server?

2

Why should message ordering use server-assigned timestamps rather than client timestamps?

3

What is the purpose of an idempotency key in WebSocket messaging?

4

In a heartbeat-based presence system, what is the tradeoff of a longer heartbeat interval?

5

What is the token bucket algorithm's advantage over a fixed window rate limiter?

6

What happens if a reconnecting client sends a lastSeq that is older than the server's message buffer?

Coding Challenges

1
1

Implement Message Catch-Up with Sequence Numbers

Build a Node.js WebSocket server (using the ws package) that assigns an incrementing sequence number to every broadcast message and maintains an in-memory ring buffer of the last 200 messages. On connection, clients send { type: 'catch_up', lastSeq: N }. The server replays all messages with seq > N from the buffer. If lastSeq is -1 (new client) or if the gap exceeds the buffer, send { type: 'full_state', messages: [...all buffer...] } instead. Implement a simple test client that connects, disconnects after receiving 10 messages, waits 2 seconds, reconnects, and verifies it receives the missed messages. Input: WebSocket connections and catch_up messages. Output: JSON messages with seq fields. Estimated time: 30 minutes.

Medium

Mini Project

1

Real-Time Live Poll with Presence and Optimistic Voting

Build a live polling application using Node.js (ws package) and vanilla JS. Features: a poll creator can POST a question with up to 4 options via REST (Express); all connected clients receive the poll via WebSocket broadcast; clients vote by clicking an option, which sends a vote event with an idempotency key preventing double votes; the server enforces one vote per socket per poll and broadcasts updated vote counts to all clients; votes are shown optimistically on click (increment count locally) and reconciled when the server broadcast arrives; a live presence counter shows how many users are currently connected; if a user disconnects and reconnects, they receive the current poll state and their own vote status via a catch-up message; votes and the current poll are stored in a simple in-memory object (no database required). Write a Jest test for the server-side deduplication logic.

Hard