Architecture Overview
The gateway runs as a persistent WebSocket server that:- Accepts client connections over WebSocket
- Routes inbound messages from messaging platforms to the appropriate agents
- Manages session state and concurrency
- Handles tool execution and approval workflows
- Coordinates plugin lifecycle and channel health monitoring
Core Components
WebSocket Runtime
The gateway uses a binary framing protocol for efficient real-time communication. All messages are validated using Ajv schemas defined insrc/gateway/protocol/.
Runtime State
The gateway maintains centralized runtime state including:- Active WebSocket connections
- Channel plugin registry
- Model catalog (LLM providers and models)
- Execution approval manager
- Node registry (connected devices)
Gateway Methods
The gateway exposes RPC-style methods over WebSocket for:Chat Operations
Chat Operations
chat.send- Send message to agentchat.abort- Cancel running agent interactionchat.history- Retrieve conversation historychat.inject- Inject system message into session
Agent Management
Agent Management
agents.list- List configured agentsagents.create- Create new agentagents.update- Update agent configurationagents.delete- Remove agentagents.files.*- Manage agent workspace files
Session Management
Session Management
sessions.list- List active sessionssessions.get- Get session detailssessions.reset- Clear session historysessions.send- Send message to specific session
Channel Operations
Channel Operations
channels.status- Get channel connection statuschannels.logout- Disconnect from messaging platform
Configuration
Configuration
config.get- Retrieve configurationconfig.set- Update configuration valueconfig.patch- Apply configuration patchconfig.schema- Get configuration schema
Event Flow
Client Connection
Client establishes WebSocket connection to gateway (default:
ws://localhost:18789)Startup Sequence
The gateway initializes subsystems in this order:- Config Loading - Read and validate
~/.simpleclaw/config.yaml - Plugin Discovery - Load channel and provider plugins
- Model Catalog - Index available LLM providers and models
- Channel Initialization - Connect to messaging platforms
- WebSocket Server - Start listening for client connections
- Maintenance Timers - Schedule health checks and cleanup tasks
- Discovery Services - Enable Tailscale/local network discovery
Health & Monitoring
The gateway exposes health information via:- Health Snapshots - Cached status of all subsystems (channels, agents, cron jobs)
- Presence Versioning - Incremental version numbers for change detection
- Channel Health Monitor - Periodic probes of messaging platform connectivity
- Diagnostic Events - Structured logging for debugging
Configuration
Gateway mode:
local (single-user) or shared (multi-user)Bind address:
loopback (127.0.0.1), all (0.0.0.0), or specific IPWebSocket server port
Optional TLS configuration for secure WebSocket connections
Best Practices
Connection Pooling
Reuse WebSocket connections instead of creating new ones per request
Error Handling
Always handle connection errors and implement reconnection logic
Message Validation
Validate all messages against protocol schemas before sending
Health Monitoring
Regularly check gateway health and channel status
The gateway runs as a single process. On macOS, it’s managed by the menu bar app. On Linux/cloud deployments, use systemd or process managers like PM2.