Event Routing
The server runtime implements a flexible event routing system that determines how events are distributed to connected peers.Routing Flow
Route Middleware
Middleware functions intercept events and make routing decisions.Middleware Interface
Custom Middleware Example
Routing Policies
Policies provide declarative access control without custom middleware code.Policy Configuration
Policy Examples
Plugin Whitelist
Label-Based Access Control
Combined Policy
Route Destinations
Events can specify destinations using module names or label selectors.Destination Types
Specifying Destinations
By Module Name
By Module Instance
By Labels
In Event Data
Destinations can also be specified in the data payload:Bypass Mode
Devtools peers can bypass routing policies for debugging and monitoring.Enabling Bypass
A peer is considered a devtools peer if:- It has the label
devtools=trueordevtools=1, OR - Its module name contains “devtools”
Disabling Bypass
Label Matching
Label selectors support exact matching with AND logic.Built-in Routing Rules
Self-Exclusion
Events are never sent back to the originating peer:Module Registry Access
The routing context provides access to the module registry:Performance Considerations
Minimize middleware chain
Minimize middleware chain
Keep middleware functions lightweight. Heavy computation or I/O operations will block event processing.
Use policies over middleware
Use policies over middleware
Built-in policies are optimized for common access control patterns. Use custom middleware only when policies are insufficient.
Cache label lookups
Cache label lookups
If performing complex label matching, cache results within middleware to avoid repeated computation.
Consider event volume
Consider event volume
High-frequency events benefit from explicit destinations rather than broadcast-then-filter approaches.
Debugging Routing
Enable WebSocket logging to trace routing decisions:- Received events with peer information
- Routing decisions (broadcast, targets, drop)
- Delivery status to each peer
- Connection health (heartbeat, timeout)
Next Steps
WebSocket Protocol
Review the WebSocket message protocol
Server SDK
Use the client SDK with built-in routing
