NodeJSNetworking in Node.js

Networking in Node.js

Node was built for the network. Its non-blocking, event-driven core (the same event loop and streams you've already met) is exactly what makes it excel at handling thousands of simultaneous connections on a single thread. Before diving into HTTP, it pays to understand the layers underneath it — TCP, UDP, and DNS — and the small family of core modules Node provides for each.

The networking stack, briefly

Network communication is layered. Each Node module maps onto a layer, and the higher-level ones are built on the lower:

Layer

What it does

Node module

Application

HTTP, WebSocket, your protocol

http, https, http2, ws

Transport

Reliable streams (TCP) / datagrams (UDP)

net (TCP), dgram (UDP), tls

Naming

Hostnames → IP addresses

dns

Network/Link

IP routing, Ethernet

(handled by the OS)

HTTP is built on TCP, which is built on the OS
When you create an `http` server, Node is using a `net` (TCP) server under the hood, which in turn uses the operating system's socket APIs via libuv. Understanding `net` therefore demystifies `http` — an HTTP request is just bytes parsed out of a TCP stream. You don't *need* `net` for web work, but knowing it exists clarifies what HTTP actually is.
TCP vs UDP — the fundamental choice

TCP (net)

UDP (dgram)

Connection

Connection-oriented (handshake first)

Connectionless (just send)

Reliability

Guaranteed delivery, retransmits

Best-effort — packets can drop

Ordering

In-order, byte stream

No ordering guarantee

Overhead

Higher (acks, state)

Minimal

Model

A continuous stream of bytes

Discrete independent packets

Use for

HTTP, databases, anything needing correctness

Video/voice, gaming, DNS, metrics

TCP is a byte stream, NOT a message stream
The most common TCP misconception: that one `socket.write('hello')` arrives as one `data` event reading `'hello'`. **It does not.** TCP guarantees the bytes arrive in order, but not how they're grouped — your message may be split across multiple `data` events or merged with the next. To send discrete messages over TCP you must add your own **framing** (length prefixes or delimiters). UDP, by contrast, preserves message boundaries but may lose or reorder them.
Why Node handles concurrency so well here

A traditional thread-per-connection server spends most threads blocked waiting on slow network I/O. Node instead registers a non-blocking socket with the OS and gets an event when data is ready — so one thread juggles tens of thousands of mostly-idle connections. This is the non-blocking I/O model applied to the network, and it's Node's original reason for being.

I/O-bound, not CPU-bound
This model shines when connections spend their time *waiting* (the typical web/API workload). It does **not** speed up CPU-heavy work — a single request doing heavy computation still blocks the one thread and stalls every other connection. For that you reach for worker threads or clustering; networking concurrency and CPU parallelism are different problems.
The modules this section covers

After these, the HTTP section builds the web server you probably came for — but now you'll know what's happening one layer down.

A taste: an echo server in 6 lines

JS
import { createServer } from 'node:net'

// A TCP server that echoes back whatever it receives:
createServer((socket) => {
  socket.write('Welcome!\n')
  socket.pipe(socket)            // pipe input straight back to output
}).listen(4000, () => console.log('echo server on :4000'))
A socket is a Duplex stream
Notice `socket.pipe(socket)` — because a TCP socket is a [duplex stream](/nodejs/duplex-transform-streams), you can pipe its readable side into its own writable side to echo data. Everything you learned about streams applies directly to network sockets: backpressure, `data`/`end` events, `pipeline`. The next page explores this in full.
Next
Build real TCP servers and clients, and learn message framing: [TCP Servers (net Module)](/nodejs/net-module).