Back to Portfolio

AfterLink Protocol

A high-performance custom application-layer binary TCP protocol for Node.js, designed for sub-millisecond real-time communication.

10-Byte Header Frame Layout

B0 0xAF Magic
B1 0x01 Version
B2 0x02 Type
B3 – B6 32-bit uint
Payload Length Length in Bytes
B7 – B8 16-bit uint
Route ID Channel ID
B9 0x00 Flag

Why AfterLink?

Standard protocols like HTTP and WebSockets carry heavy frame overhead (frame encoding, masking keys, padding) which adds serialization latency. AfterLink solves this by using a fixed 10-byte header frame followed by raw buffer streams.

By avoiding base64 encoding and text serialization, AfterLink reads and writes binary data straight to Node.js TCP Sockets, maintaining sub-millisecond response times even under high throughput.

Under the Hood Highlights
  • Strict schemas: Integrated with Zod for binary payloads.
  • Persistent: Fully multiplexed, single socket channel.
  • Native Pub/Sub: High-performance event router in C++.

Performance Benchmarks

Throughput comparison under peak workloads (messages processed per second, local network testing)

AfterLink (TCP Binary) 30,167 msg/sec
Standard WebSocket (ws) 17,200 msg/sec
Socket.IO (Engine.IO) 12,000 msg/sec

Feature Matrix

Feature AfterLink WebSocket gRPC
Framing format Binary (10B fixed header) Text/Binary (Variable mask) Protobuf stream (HTTP/2)
Validation layer Zod Schemas (Runtime) None (Manual validation) Protobuf schemas (Compile)
Latency Sub-1ms (0.033ms avg) 1-3ms 2-5ms
Pub/Sub support Native & Multiplexed Third-party required Requires stream channels

Lifecycle Flow

01

TCP Connection

Clients connect directly to the TCP socket server. Handshake verifies magic bytes (0xAF) to establish tunnel.

02

Frame Decoding

Data is read as buffer blocks. The header parser slices 10-byte offsets to extract length, route, and flag properties.

03

Zod Validation & Route

Payloads are validated against schemas on designated routes. Handled outputs are stream-broadcast back immediately.

Quick Start Example

afterlink-example.js JavaScript (Node.js)
// 1. Import AfterLink server
import { createServer } from 'afterlink';
import { z } from 'zod';

// 2. Initialize and declare schema validations
const server = createServer({ port: 8080 });

const UserMessageSchema = z.object({
  userId: z.string(),
  content: z.string()
});

// 3. Listen on routes
server.route('/chat', UserMessageSchema, (msg, socket) => {
  console.log(`Message received from ${msg.userId}: ${msg.content}`);
  
  // Broadcast response
  server.broadcast('/chat', {
    userId: 'system',
    content: 'Message processed successfully.'
  });
});

server.start(() => console.log('AfterLink listening on 8080'));

Ready to integrate?

Get AfterLink on npm registry or clone the codebase on GitHub.