Skip to main content
FS
From Scratch

Relay

Completed⭐ Featured4 min read

Tech Stack

Node.jsTypeScriptTCPBinary ProtocolsEvent-Driven Architecture

A Real-Time TCP Message Relay Engine Built to Master Node.js at the Systems Level

Relay is not a chat app. It's not a UI project. It's infrastructure, a message relay server that demonstrates deep understanding of Node.js at the systems level. This is about binary protocols, TCP streams, backpressure, and event-driven architecture using only Node.js core modules: net, events, and buffer.

What is Relay?

Relay is a real-time TCP message relay engine designed for learning and mastering low-level networking concepts in Node.js. It implements a custom binary frame-based protocol for efficient message transmission, handles TCP stream complexities like backpressure and packet fragmentation, and manages room-based message routing with strict state machines.

This project exists to prove that you can build production-grade networking infrastructure by understanding the fundamentals, not by importing packages.

The Core: Binary Protocol Design

Relay implements a custom binary protocol for maximum efficiency:

Frame Structure:
| length (4B) | version (1B) | type (1B) | flags (1B) | payload (variable) |

All numeric fields use Big Endian (network byte order) for proper TCP/IP compliance.

Message Types:

  • HELLO: Initial connection handshake
  • JOIN_ROOM: Subscribe to a message room
  • LEAVE_ROOM: Unsubscribe from a room
  • MESSAGE: Send message to current room
  • HEARTBEAT: Keep-alive signal
  • ERROR: Error responses

Why Binary?

  • Minimal overhead compared to JSON or text protocols
  • Efficient parsing and serialization
  • Type-safe message boundaries
  • Production-ready performance

Connection Lifecycle

Relay implements a strict state machine for connection management:

INIT → OPEN ⟷ DRAINING → CLOSING → CLOSED

This ensures proper handling of edge cases, graceful shutdowns, and prevents resource leaks.

Technical Deep Dives

TCP Stream Handling Relay demonstrates mastery of TCP stream complexities:

  • Backpressure Management: Properly handles socket buffers and prevents memory exhaustion
  • Incremental Parsing: Buffers partial frames across multiple packets
  • Packet Boundaries: Correctly handles messages split across TCP packets
  • Flow Control: Implements proper pause/resume semantics

Event-Driven Architecture Built on Node.js EventEmitter pattern:

  • Clean separation between transport and protocol layers
  • Loosely coupled components for testability
  • Proper error propagation and handling
  • Memory-safe event listener management

Room-Based Routing Efficient message routing system:

  • O(1) room lookups using Map data structures
  • Broadcast optimization for multi-client rooms
  • Automatic cleanup of empty rooms
  • Client isolation and message privacy

Project Structure

The codebase is organized as a monorepo demonstrating modern Node.js project architecture:

relay/
├── apps/
│   ├── server/        # TCP server (net.createServer)
│   └── client/        # CLI demo client
├── packages/
│   ├── protocol/      # Frame encoding/decoding (socket-agnostic)
│   └── transport/     # Connection management & backpressure
├── scripts/           # Chaos, load, and fragmentation tests
└── docs/              # Protocol specification & architecture

Key Design Decisions:

  • Protocol Package: Socket-agnostic encoding/decoding, fully testable
  • Transport Package: Connection lifecycle, backpressure, state machine
  • Server App: Room management, message routing, production logging
  • Client App: Interactive CLI for testing and demonstration

Testing & Reliability

Relay includes comprehensive testing infrastructure:

Chaos Testing

  • Random disconnections during message transmission
  • Network instability simulation
  • Resource exhaustion scenarios

Load Testing

  • Thousands of concurrent connections
  • High-throughput message broadcasting
  • Memory leak detection

Fragmentation Testing

  • Deliberately split messages across packets
  • Verify incremental parser correctness
  • Edge case handling validation

Educational Value

Relay serves as a masterclass in several advanced topics:

Binary Protocol Design Learn how to design efficient wire protocols, handle endianness, implement framing, and parse incrementally.

TCP Stream Programming Master backpressure handling, buffer management, packet boundaries, and flow control.

Event-Driven Architecture Understand EventEmitter patterns, proper error handling, memory management, and component decoupling.

State Machine Implementation Build robust connection lifecycle management with proper state transitions and edge case handling.

Node.js Internals Deep dive into core modules (net, buffer, events) with zero dependencies and production patterns.

Use Cases

Learning Platform The primary purpose of Relay is education. The codebase is extensively documented and structured to teach systems programming concepts.

Production Infrastructure While built for learning, Relay's architecture is production-ready and can be deployed for real-time message routing.

Protocol Foundation Use Relay as a starting point for custom binary protocol implementations in your own projects.

Interview Preparation Demonstrate deep understanding of networking, protocols, and Node.js internals.

Running Relay

Server:

bash
1npm run server          # Start on localhost:4000
2npm run server:debug    # Verbose logging

Client:

bash
1npm run client                    # Connect to localhost:4000
2npm run client <host> <port>      # Connect to custom server

CLI Commands:

  • join <room> - Join a message room
  • send <message> - Send to current room
  • leave <room> - Leave a room
  • heartbeat - Send keep-alive
  • help - Show commands

The Philosophy

Relay is a testament to the belief that understanding fundamentals is more valuable than memorizing frameworks. It's about knowing how things work at the protocol level, at the TCP level, at the buffer level. This knowledge is transferable, timeless, and empowering.

In a world of high-level abstractions, Relay goes low-level and that's where the real learning happens.


Notes

  • Pure Node.js core modules (no external dependencies)
  • Custom binary protocol implementation
  • Production-grade TCP stream handling
  • Comprehensive testing suite included
  • Extensive documentation for learning
  • Monorepo structure demonstrating modern Node.js architecture
  • CLI client for easy testing and demonstration
  • Suitable for both educational and production use

Project Timeline

Created:27/12/2025
Last Updated:27/12/2025