Skip to content

API Reference

Complete constructor properties, method signatures, middleware chains, error tables, and TypeScript definitions for AfterLink.

Server Class Reference

Instantiated via new Server(options) or the wrapper helper createServer(options).

Server Options Parameters

Option Type Default Description
portnumber8080TCP port configuration range.
hoststring'0.0.0.0'Network interface address mapping.
idleTimeoutnumber60000Heartbeat delay threshold in milliseconds.
maxConnectionsnumber10000Peak concurrent active sockets allowance limits.
tlsobjectnullPass key, cert buffers to activate secure TLS handshakes.
server.on() method
Callback Hooks

Registers a callback listener for specific route requests or system handshake events, optionally verifying with Zod validation models.

Signature

server.on(route: string, handler: (socket, payload) => void, schema?: ZodSchema)
server.use() method
Middleware

Injects global middleware functions into the connection processing stack (e.g. JWT validations or packet logging).

Signature

server.use(middleware: (socket, frame, next) => void)
server.publish() method
Pub/Sub

Pushes pub/sub broadcasts directly onto channel topics. Server manages delivery lists and relays MSG_BROADCAST (0x05) frames automatically.

Signature

server.publish(channel: string, payload: any)
server.broadcast() method
Fan-Out

Transmits identical raw payload buffers to all active client sockets regardless of routing channel configurations.

Signature

server.broadcast(type: string, payload: any)
server.close() method
Shutdown

Closes raw TCP bindings gracefully, terminating keepalive heartbeats, and closing active connections correctly.

Signature

server.close(callback?: () => void): Promise<void>

Client Class Reference

Instantiated via new Client(options) or the wrapper helper createClient(options).

Client Options Parameters

Option Type Default Description
hoststring'127.0.0.1'Host address of target AfterLink server.
portnumber8080Destination binding TCP port range.
autoReconnectbooleantrueTriggers socket re-connections upon socket drops.
maxRetriesnumber10Limits reconnection sequences before throwing errors.
client.connect() / disconnect() method
Connection Lifecycle

Triggers asynchronous handshakes to establish active socket bindings, or tears down established socket connections gracefully.

Signature

client.connect(): Promise<void>  |  client.disconnect(): Promise<void>
client.request() method
RPC Loop

Dispatches structured RPC request packets (MSG_REQUEST) and returns matching Promise response elements resolving when server returns matching seq IDs.

Signature

client.request(route: string, body: any, options?: object): Promise<any>
client.subscribe() method
Pub/Sub

Binds call functions to specific pub/sub streams. Instructs server routing layers by pushing MSG_SUBSCRIBE frames immediately.

Signature

client.subscribe(channel: string, callback: (payload, channel) => void): void
client.publish() method
Pub/Sub

Publishes messages onto specified routing channels. Packages fields and transmits a single MSG_PUBLISH (0x03) packet.

Signature

client.publish(channel: string, payload: any): void
client.stream() method
Streaming

Establishes interactive, multi-frame streaming pipelines. Dispatches structured MSG_STREAM packets sequentially and ends with a MSG_STREAM_END marker.

Signature

client.stream(route: string, body: any): Promise<ReadableStream>

Protocol Error Codes

Error Code Error Constant Diagnostic Description & Root Cause
100 ERR_MAGIC_MISMATCH Invalid magic identifier bytes. Sockets from hostile scans or HTTP requests.
101 ERR_VERSION_UNSUPPORTED The client version constant is incompatible with this server.
400 ERR_BAD_FRAME Corrupt framing format structure, length offsets mismatch.
401 ERR_UNAUTHORIZED Missing, invalid or expired handshake JWT authentication tokens.
404 ERR_ROUTE_NOT_FOUND No matching method path listener exists on this server.
422 ERR_VALIDATION_FAILED The frame payload model does not conform to the registered Zod validation schema.
429 ERR_RATE_LIMITED Connection request counts exceeded maximum specified configurations.
500 ERR_INTERNAL_SERVER Unhandled exception occurred in server-side handler code.

TypeScript Declarations

Full TypeScript definitions for the core interfaces: