How Node really works · Chapter 32 of 34

Project: the riders' chat room

By the end of this chapter you can build a TCP server where many people talk to each other at once, and that keeps going whoever leaves, however they leave. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Projects
  • 6 tasks
  • Node.js
  • In the all labs plan

The first 2 chapters of How Node really works are free. This one comes with the all labs plan.

Why you need this

Ravi's riders talk in a WhatsApp group, and order updates get lost between the good morning messages. He wants a chat room for riders only: join with your name, talk to everyone, send one rider a private note, leave when the shift ends. Kabir wants it on plain TCP with node:net, and adds one rule: a phone that dies in a basement must not crash the room.

In chapter 19 each connection to the printer was its own conversation. A chat room is different: what one rider types must reach the other sockets, so connections share state, and the room has to notice every rider who goes, even one who never says goodbye.

What you build: 6 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    createChatServer: let riders join with a unique name

    Write createChatServer(). It returns a net.Server that is not listening yet (the caller calls listen). Everything it sends is one line of text ending in '\n'.

    As soon as a connection opens, send it WELCOME. The first line that comes back is the rider's name: it must be one word, not empty or only spaces, and not taken by a rider in the room. Otherwise send That name is not available, try another, and wait for another name.

    When the name is good, the rider joins: they get Hi <name>, <n> online, where n counts everyone in the room including them, and every other rider in the room gets * <name> joined. The new rider does not get their own joined line.

  2. Task 2

    createChatServer: send each message to everyone but the sender

    After joining, every line that does not start with / is a message: every other rider in the room gets <name>: <the line>. The sender does not get it back, and a connection still choosing its name gets nothing at all until it joins (its first lines are only WELCOME and then its Hi line).

    A blank line is ignored: nobody gets anything.

  3. Task 3

    createChatServer: send a private note with /msg

    /msg <name> <text> sends [private] <sender>: <text> to that one rider only, with the text as it was typed after the name. When nobody of that name is in the room, the sender gets No rider named <name>.

    Any other line starting with / is not a message: the sender gets Unknown command <the first word>, for example Unknown command /dance, and nobody else gets anything.

  4. Task 4

    createChatServer: say bye and hang up on /quit

    /quit sends Bye <name> to the rider and then the server ends that connection. Every other rider gets * <name> left. The name is free again at once, so someone can join with it, and the online count goes down by one.

  5. Task 5

    createChatServer: survive a rider dropping off without warning

    A rider can also leave by closing the connection or by losing it (a reset, when the phone dies). Then every other rider gets * <name> left and the name is free again, exactly as with /quit, and the server keeps running for everyone else.

    Every socket must have an error listener, so a reset never crashes the server. Someone who leaves before choosing a name is not announced, and nobody is ever announced as leaving twice.

  6. Task 6 · interview question

    Interview: createChatServer, rebuild lines that TCP splits or joins

    The data events do not match the lines a rider typed. One line can arrive in two chunks (a name sent as Mee and then ra), and two lines can arrive in one chunk. Handle only whole lines, in order, each one exactly once, and remove a '\r' at the end of a line (telnet ends lines with '\r\n').

What you will learn

  • Shared and per-connection state: what lives outside the connection callback, and what lives inside it.
  • Lines, not chunks: why a name can arrive in two halves.
  • Commands: reading a line that starts with /.
  • Every way out: end, destroy and a reset, and the one event they all finish with.

More in Projects

Build "Project: the riders' chat room" yourself

Tapri’s order server must never stall at the 4 pm rush. You learn why it would. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of How Node really works are free. This one comes with the all labs plan.