How Node really works · Chapter 19 of 34

TCP with node:net

By the end of this chapter you can write TCP servers and clients with node:net, and cut a stream of bytes back into whole messages. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Networking
  • 5 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

The kitchen printer at Tapri is older than the app. It has no web page and no JSON, only a network cable and a habit: open a connection, send PRINT masala chai x2 and a newline, and it answers OK 14. Ravi wants the order server to talk to it, and a fake printer too, so the team can test at home.

fetch and node:http cannot help: they speak HTTP, and the printer does not. Underneath every HTTP request is a TCP connection, a pipe of bytes in each direction. node:net gives you that pipe with nothing on top, so deciding where one message ends is now your job.

What you build: 5 tasks

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

  1. Task 1 · interview question

    Interview: createLineReader, rebuild whole lines from TCP chunks that split and merge

    Write createLineReader(onLine). It returns a function, push, that you call with each chunk of text. push calls onLine once for every complete line, in order, without its '\n' and without a '\r' at its end. Text after the last newline waits until later chunks complete it, and an empty line is passed on as ''.

    Every reader keeps its own leftover text: two readers never mix.

  2. Task 2

    createPrinterServer: answer PRINT and PING from the kitchen terminals

    Write createPrinterServer(onTicket), where onTicket does nothing when left out. It returns a net.Server that is not listening yet. On every connection it reads commands, one per line (several can arrive in one chunk), and answers each with one line ending in '\n':

    PRINT <ticket> answers OK <job> and calls onTicket(ticket, job); PING answers PONG; a blank line gets no answer; anything else answers ERR unknown command.

    Job numbers start at 1 and count for the whole server, across all its connections.

  3. Task 3

    createPrinterServer: keep a ticket whole when it arrives in pieces

    The printer must cope with a command that arrives in several chunks, with a '\r\n' line ending, and with a ₹ cut in half between two chunks: PRINT chai ₹40 must reach onTicket as 'chai ₹40' and answer OK with its job number.

  4. Task 4

    createPrinterServer: say BYE and hang up on QUIT

    QUIT answers BYE, and then the printer ends that connection. Lines that came after QUIT, even in the same chunk, are ignored: no answer, no ticket, no job number. Other connections keep working.

  5. Task 5

    sendToPrinter: send tickets to the printer and collect every reply

    Write sendToPrinter(port, lines). It connects to 127.0.0.1 on port and, once connected, writes every line followed by '\n'. It returns a promise of the replies, one per line sent, in order, without their line endings. The printer may send its replies in awkward pieces.

    Once every reply is in, it ends the connection itself: the printer must see the end. It rejects on a socket error, or when the printer hangs up before answering everything.

What you will learn

  • Servers and sockets: net.createServer, createConnection, and a socket as a duplex stream.
  • Framing: TCP delivers bytes, not messages, so you cut them into lines yourself.
  • setEncoding: keeping a ₹ whole when it is cut between chunks.
  • Errors and hanging up: end, 'error', and closing a server.

More in Networking

Build "TCP with node:net" 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.