System Architecture: Data Flow and Components
This document shows how data moves through the system: Client, Runner, Router, Middleware, ApiModules, LightweightModules, Lightweight Handlers, and Handles.
Legendโ
- WS: WebSocket connection managed by the Runner via the Connector
- Router: multiplexes messages to modules and handlers using rules
- Middleware: pre-/post-processing for inbound/outbound WS messages
- ApiModule: full-featured module with commands, responses, and a Handle
- LightweightModule: background task, receives routed WS messages, no command/response
- Lightweight Handler: global stateless callback receiving every WS message
End-to-end Overviewโ
The data flow consists of:
- Inbound Path: WebSocket โ Connector โ Runner โ Middleware (inbound) โ Router โ Lightweight Handlers, LightweightModules, ApiModules via rules
- Outbound Path: ApiModule via Handle, LightweightModule โ Runner โ Middleware (outbound) โ Connector โ WebSocket
ApiModule internals: commands, responses, and routingโ
- The builder registers an M::Handle in a shared map.
Client.get_handle::<M>()returns it. - The module runs its own loop, reading commands and WS messages, emitting responses.
LightweightModule internals: simple routed loopโ
- No Handle or command/response. Great for keep-alive, monitoring, or augmenting state.
Lightweight Handlers: global tapโ
- Registered callbacks executed for all messages (e.g., logging).
Middleware positioningโ
- Middleware can inspect/modify inbound and outbound traffic globally.
ClientBuilder, Runner, and module registration (sequence)โ
Inbound message flow (detailed)โ
Outbound message flow (detailed)โ
Reconnect flow (high level)โ
Where to look in the codeโ
- Core:
crates/core/srcbuilder.rs: ClientBuilder (module registration, routing rules)client.rs,connector.rs,routerinsidebuilder.rstraits.rs: ApiModule, LightweightModule, AppState, Rule, ReconnectCallbackmiddleware.rs: Middleware stack
- PocketOption integration:
crates/binary_options_tools/src/pocketoptionmodules/*: concrete modules (subscriptions, trades, server_time, raw, ...)pocket_client.rs: registers modules and exposes get_handle helpers