Overview
The@proj-airi/server-runtime package provides a WebSocket-based server runtime that enables real-time communication between AIRI modules, plugins, and clients. It handles authentication, routing, heartbeat management, and module registry synchronization.
Installation
Key Features
- WebSocket Communication: Real-time bidirectional communication using H3 and crossws
- Authentication: Token-based authentication for secure connections
- Module Registry: Automatic discovery and synchronization of connected modules
- Routing Middleware: Flexible routing with policy-based access control
- Heartbeat Management: Automatic connection health monitoring
- Event Broadcasting: Intelligent event routing to appropriate peers
Basic Usage
Configuration
Unique identifier for the server instance. Defaults to a generated nanoid.
Authentication token required for client connections. If not provided, authentication is disabled.
Application log level:
'trace', 'debug', 'log', 'warn', 'error'. Defaults to 'log'.Log format:
'pretty' or 'json'. Defaults to 'pretty'.WebSocket-specific log level. Defaults to app log level.
Custom routing middleware functions for event filtering and routing decisions.
Allow devtools peers to bypass routing policies. Defaults to
true.Routing policy for plugin and label-based access control.
Heartbeat timeout in milliseconds. Defaults to
60000 (60 seconds).Custom heartbeat message. Defaults to
MessageHeartbeat.Pong.Return Value
H3 application instance with WebSocket handler configured.
Function to close all connected peer connections.
Event Types
The server handles the following WebSocket event types:transport:connection:heartbeat- Connection health monitoringmodule:authenticate- Client authenticationmodule:announce- Module registration and announcementregistry:modules:sync- Module registry synchronizationui:configure- Module configuration from UI- Custom events - Routed to appropriate peers based on routing rules
Environment Variables
The server runtime supports the following environment variables:SERVER_INSTANCE_ID- Server instance identifierAUTHENTICATION_TOKEN- Default authentication tokenLOG_LEVEL- Default log levelLOG_FORMAT- Default log format
Architecture
The server runtime follows a peer-to-peer architecture where:- Clients connect via WebSocket to
/ws - Authentication occurs if a token is configured
- Module announcement registers the client in the module registry
- Events are routed based on destinations and routing policies
- Heartbeats maintain connection health
Next Steps
WebSocket Protocol
Learn about the WebSocket protocol and message format
Event System
Explore the event system and routing mechanisms
