# Mixed Self-check — WebSockets & Server-Sent Events

Source: https://www.skillbyai.com/en/realtime-web/rt-self-check

> Test yourself on scenarios that combine transport choice, protocol details, scaling and security.

## How to use this self-check

The scenarios below mix ideas from the whole course on purpose, because real problems never announce which chapter they belong to. For each one, decide on your answer **before** reading the explanation, and say *why* in one sentence; if you cannot give the reason, revisit the named section. Scenario thinking follows a fixed order: first identify the **direction and rate** of data (one-way or two-way, rare or constant), then the **failure mode** being described (drop, slowness, overload, abuse), then the **layer** where it happens (browser, proxy, server, broker). Many production incidents are a mismatch between those layers, such as a heartbeat longer than a proxy timeout, or a broadcast loop that never checks `bufferedAmount`. Work through the tips that follow as mini-questions, then finish with the quiz. If you get something wrong, write a one-line note of the correct rule; those notes become your own revision sheet.

## Scenario 1: the 60-second drop

Sockets close with 1006 after exactly 60 seconds of silence, only in production. **Answer:** a proxy or load-balancer idle timeout. Add heartbeats more often than 60 seconds, or raise the proxy's read timeout (Section 3 and Section 6).

## Scenario 2: events arrive in bursts

An SSE stream works locally but behind nginx events appear in clumps every few seconds. **Answer:** proxy buffering or compression. Set `proxy_buffering off` or send `X-Accel-Buffering: no`, and exclude `text/event-stream` from gzip (Section 3).

## Scenario 3: half the users miss messages

After scaling from one to two servers, users on different servers cannot see each other's chat. **Answer:** in-memory rooms are per process. Fan out through a broker such as Redis pub/sub (Section 7).

**Quiz:** A trading dashboard pushes 40 price updates per second. Memory on the server climbs whenever users on weak mobile networks connect. What is the best first fix?

- [ ] Switch every client to long polling
- [ ] Increase the server's RAM
- [ ] Turn on perMessageDeflate for all sockets
- [x] Check `bufferedAmount` and coalesce updates, sending only the latest prices periodically

*Answer:* Check `bufferedAmount` and coalesce updates, sending only the latest prices periodically. This is backpressure from slow consumers. Coalescing latest-value data and skipping sends to sockets with large buffers bounds memory; more RAM only delays the problem.
