P4: Build a chat

: the board shows one front door that sends each caller to one of several servers. A chat is the same shape with one change: the callers stay connected, so the servers must also talk to each other. The last section of this lesson is about that change.
Asha Rao (a made-up learner, so her numbers are teaching values) has the full-stack app from P3: A full-stack app, end to end. A friend tries it with her and says: “Nice. Can I see when someone replies without pressing refresh?” Her app cannot do that. A browser asks a question and the server answers. The server cannot start a sentence. If Asha makes the page ask “anything new?” every second, 1,000 open pages make 1,000 requests every second, and almost every answer is “no”.
A chat is a set of rooms. A person joins a room, sends a message, and everyone else in the room sees it within a moment. By the end of this lesson you will be able to build one that does five jobs: keep a connection open, copy each message to everyone in the room, show who is online, give back what a person missed, and drop a person who has gone silent. You will also be able to say what changes when one server is not enough.
The history (optional)
Jarkko Oikarinen wrote the first Internet Relay Chat client and server in summer 1988, while working at the University of Oulu in Finland. He gave the date as August 1988 and said the exact day is not known. His server, tolsun.oulu.fi, was the first. By mid-1989 there were about 40 IRC servers worldwide, and the protocol was written down as RFC 1459 in May 1993. IRC already had rooms (called channels) and a list of who is in them. The WebSocket protocol, which lets a browser do the same over a long-lived connection, was published as RFC 6455 in December 2011. Nothing later depends on this history.
Before you start. P3: A full-stack app, end to end gave you a login with a session cookie, rows kept apart by user, and a health check. The customer who won't hang up (polling, SSE and WebSockets) compared polling, server-sent events and WebSockets, and chose a connection that stays open. S4 · Build an HTTP Server (without net/http) read a request off a socket by hand. Redis: One Thread, Many Clients showed one thread serving many sockets by never waiting on one of them, and this server does the same with Python’s asyncio. WhatsApp, the case study told the story of the mailbox that holds messages for a phone that is away.