Expand description
§Brain Bridge
External service relay — WebSocket client that connects Brain to a remote messaging gateway and relays inbound messages through Brain’s signal processing pipeline.
§Protocol
BridgeClientconnects to the configuredurlvia WebSocket.- Each inbound text frame is parsed as a JSON-encoded
BridgeMessage. Unparseable text frames are logged and skipped; the connection stays open. Binary frames are silently ignored. - WebSocket
Pingframes are answered withPongautomatically.Closeframes trigger the reconnect loop after the configured backoff. - A caller-supplied
handleris invoked for each parsed message and must return aBridgeMessageresponse (the response keeps the original message’sidif built viaBridgeMessage::reply). - The response is serialised and sent back as a text frame. Failure to send tears down the connection and triggers the reconnect loop; serialisation failure logs and drops the response only.
- On disconnect, the client waits
initial_backoff_ms * 2^attempt(capped atmax_backoff_ms) before retrying. WithBridgeConfig::max_reconnect_attemptsset, the loop returnsBridgeError::MaxRetriesExceededonce the cap is reached; withNone, retries continue indefinitely.
BridgeClient::connect_and_relay_bidirectional adds an optional
proactive-push channel: each BridgeMessage received on
proactive_rx is serialised and pushed to the gateway alongside the
normal request/response flow. If the broadcast channel lags, lagged
events are logged and dropped; if it closes, the client continues in
relay-only mode.
§Usage
let client = BridgeClient::new("ws://gateway.example.com/brain", BridgeConfig::default());
client.connect_and_relay(|msg| async move {
BridgeMessage::reply(&msg, format!("Echo: {}", msg.content))
}).await?;Structs§
- Bridge
Client - WebSocket client that relays messages between Brain and a remote gateway.
- Bridge
Config - Configuration for
BridgeClientreconnection behaviour. - Bridge
Message - A message exchanged between Brain and the remote gateway.