Overview
The@proj-airi/server-sdk package provides a client SDK for connecting to the AIRI server runtime. It handles WebSocket communication, authentication, heartbeat management, and event routing.
Installation
Quick Start
Connect to a server runtime:Client Class
Constructor
Create a new client instance.string
default:"ws://localhost:6121/ws"
WebSocket server URL
string
required
Module name for identification
string
Authentication token (if server requires auth)
MetadataEventSource
Custom module identity (auto-generated if not provided)
string[]
List of event types this module can emit
ModuleDependency[]
Module dependencies
ModuleConfigSchema
Configuration schema for this module
boolean
default:"true"
Connect automatically on construction
boolean
default:"true"
Reconnect automatically on disconnect
number
default:"-1"
Maximum reconnection attempts (-1 for unlimited)
number
default:"30000"
Heartbeat interval in milliseconds
MessageHeartbeat | string
Custom heartbeat message
(error: unknown) => void
Error handler callback
() => void
Connection close callback
(data: WebSocketEvent) => void
Handler for all incoming messages
(data: WebSocketEvent) => void
Handler for all outgoing messages
Methods
connect()
Manually connect to the server.send()
Send an event to the server.string
required
Event type identifier
T
required
Event payload data
object
Routing information
object
Event metadata (auto-populated if not provided)
sendRaw()
Send raw data without serialization.onEvent()
Register an event handler.string
required
Event type to listen for
function
required
Event handler function
offEvent()
Unregister an event handler.close()
Close the connection.TypeScript Generics
Type your custom event data:Event Types
The SDK re-exports event types from@proj-airi/server-shared:
Connection Lifecycle
The client manages connection lifecycle automatically:Authentication Flow
Heartbeat Management
Heartbeats maintain connection health:Error Handling
Reconnection with Backoff
The client implements exponential backoff:Event Routing
Route events to specific destinations:Module Registry
Receive and track connected modules:Configuration Handling
Message Interception
Intercept all messages for logging or debugging:Node.js Utilities
For Node.js environments:Complete Example
AI module connecting to server:Best Practices
Handle reconnection
Handle reconnection
Always enable
autoReconnect for production services to handle network issues gracefully.Use event IDs for tracing
Use event IDs for tracing
Include
parentId in responses to enable request-response correlation and distributed tracing.Implement heartbeat
Implement heartbeat
Configure appropriate heartbeat intervals based on your network conditions (default: 30s).
Type your events
Type your events
Use TypeScript generics to type-check event data and catch errors at compile time.
Handle authentication errors
Handle authentication errors
Listen for authentication errors and handle them appropriately (retry with new token, notify user, etc.).
Next Steps
Server Runtime
Learn about the server runtime
WebSocket Protocol
Understand the WebSocket protocol
