Skip to Content
πŸŽ‰ Welcome to my notes πŸŽ‰
Node.jsNetworking🌐 Networking in Node.js

🌐 Networking in Node.js

πŸ’‘ Introduction

Node.js was built from the ground up to be excellent at networking. Its event-driven, non-blocking I/O model is perfectly suited for handling thousands of concurrent connections with minimal memory overhead. All networking in Node.js is built upon a set of core modules that provide the fundamental building blocks for communication.

πŸš€ The dgram Module - Fast & Fire-and-Forget UDP

The dgram module provides an implementation of UDP (User Datagram Protocol) sockets.

UDP is a connectionless protocol. It sends packets of data (datagrams) without establishing a persistent connection and without any guarantee of delivery or order.

  • Analogy: Sending a postcard. You send it off without knowing for sure if or when it will arrive, or if multiple postcards will arrive in the order you sent them.

The trade-off for this lack of reliability is speed. UDP has very little overhead, making it much faster than TCP.

  • Use Cases: DNS, video and audio streaming, online gaming, and other applications where speed is more critical than perfect reliability.

πŸ”Œ The net Module - Low-Level TCP

The net module provides the foundation for creating TCP (Transmission Control Protocol) servers and clients.

TCP is a reliable, connection-oriented protocol. It guarantees that data sent from one point to another will arrive intact, in the correct order, and without errors.

  • Analogy: A telephone call. A connection must be established before you can talk, and the conversation flows in a sequential, reliable manner.

The net module allows you to work directly with sockets and streams. When a client connects to a net server, the connection object is a Duplex Stream, allowing you to both read from and write to it.

  • Use Cases: Chat servers, game servers, database connections, and any application where a persistent, reliable two-way connection is required.

🌍 The http & https Modules - The Language of the Web

The http and https modules are built on top of the net module and are used to work with the HTTP protocol, the foundation of the World Wide Web.

HTTP is a request-response protocol. A client sends a request (e.g., β€œGET /about-us”), and the server sends back a response (e.g., the HTML content of the about page).

  • http: For standard HTTP communication.
  • https: A secure version that handles SSL/TLS encryption.

When creating an HTTP server, the callback provides you with two main objects:

  1. request: A Readable Stream containing information about the incoming request (headers, body, etc.).
  2. response: A Writable Stream used to send headers and data back to the client.
  • Use Cases: Building web servers, REST APIs, or acting as an HTTP client to fetch data from other services.

πŸ“– The dns Module - The Internet’s Phonebook

The dns module provides functions for domain name resolution. Its job is to translate human-readable domain names (like google.com) into computer-readable IP addresses (like 142.250.199.14). While other modules often perform DNS lookups automatically, this module gives you direct, low-level access to the system.

  • Key Methods:
    • dns.lookup(): To resolve a domain name into an IP address.
    • dns.resolve(): To resolve different types of DNS records (e.g., MX for mail servers, TXT for text records).

βœ… Summary

  • net: For reliable, connection-based TCP networking (e.g., chat servers).
  • http/https: For the web’s request-response HTTP protocol.
  • dgram: For fast, connectionless UDP networking (e.g., game servers).
  • dns: For domain name resolution.
Last updated on