Course contentsShow
← Node.js
Lesson 510 of 1,9806. Networking and HTTP FundamentalsFree lesson

Socket Lifecycle States

Map common socket states like LISTEN, ESTABLISHED, and TIME_WAIT to server connection handling strategies.

In this lesson, you will learn how the lifecycle states of a TCP socket dictate how your Node.js server manages incoming connections.

When a client initiates communication, your socket passes through several states defined by the TCP protocol. Understanding these states allows you to write more resilient server-side logic.

The Journey of a Socket

A socket is not just "connected" or "disconnected"; it transitions through specific phases. The three most critical for a Node.js developer are:

1. LISTEN

In this state, your server’s net.Server instance is actively "listening" for incoming connection attempts on a specific port. It is essentially waiting for a client to initiate the TCP handshake. If your server is in this state, it is healthy and ready to accept new work.

2. ESTABLISHED

Once the TCP three-way handshake is complete, the socket transitions to ESTABLISHED. This is the "active" state where data flows between the client and your Node.js stream. When you handle a connection in your server's connection event, you are interacting with a socket in this state.

3. TIME_WAIT

This is often the most misunderstood state. When a connection is closed (usually by the client, or when the server finishes sending a response), the socket doesn't vanish instantly. It enters TIME_WAIT. This acts as a buffer period to ensure that any "delayed" packets from the connection are safely discarded and not confused with a new connection on the same port.

Why This Matters for Your Server

If you find your server struggling to handle high-volume telemetry or rapid connections, you might be seeing a buildup of sockets in TIME_WAIT. Because every socket uses a file descriptor, having too many sockets stuck in this lifecycle stage can prevent your server from accepting new connections, leading to "Address already in use" errors or connection timeouts.

const net = require('net');

const server = net.createServer((socket) => {
  // The socket is now in ESTABLISHED state
  socket.on('data', (data) => {
    socket.write('Data received');
  });
  
  socket.on('close', () => {
    // After closing, the socket transitions through FIN and finally TIME_WAIT
  });
});

server.listen(8080); // Server enters LISTEN state

Key Takeaway: A socket's lifecycle state determines your server's availability; managing the transition out of ESTABLISHED into TIME_WAIT is crucial for maintaining high-throughput network applications.