Index

Free, with Google

Keep going, free.

A free account opens lessons 2 and 3 of every track, 33 more lessons, and keeps your progress and review cards on every device.

Start free with Google

A chalk plan

Open everything on chalk.

Every lesson, board, video, lab and paper, and the full review decks. Yearly is $39/year.

See plansStart free instead

Module 01 · Portfolio Projects · Build It Yourself · about 17 min to read

Play it as a journeyTutor comes with a plan

P4: Build a chat

S2 - Build a Load Balancer
S2 - Build a Load Balancer · Open board

: 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.

Lesson 11 of 19 in Portfolio Projects

The rest of this lesson is part of a plan.

A plan opens every lesson, board, video, lab and paper on chalk. Yearly is $39/year.

See plansAlready a member? Sign in

Or start free: a free account opens lessons 2 and 3 of every track.

In this lesson

  • WebSocket: one HTTP request that turns into an open line
  • Fan-out: one message in, one copy to every member
  • Presence: who is in the room right now
  • Sequence number: history for the person who was away
  • Heartbeat: finding the person who vanished
  • The bytes: handshake and frames
  • Rooms, history and join
  • Backpressure: a queue per client and a limit
  • Heartbeat and cleanup
  • What the server costs, measured
  • Scaling note: when one server is not enough

Terms it explains: chat, WebSocket, fan-out, presence, sequence number, heartbeat, Common mistakes, backpressure, message bus, active vs passive, health check, reverse proxy, round-robin.

Review Portfolio Projects