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
- [
BridgeClient] connects 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 a [BridgeMessage] response (the response keeps the original message'sidif built via [BridgeMessage::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. With [BridgeConfig::max_reconnect_attempts] set, the loop returns [BridgeError::MaxRetriesExceeded] once 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
# use brainos_bridge::{BridgeClient, BridgeConfig, BridgeMessage};
# #[tokio::main] async fn main() -> anyhow::Result<()> {
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?;
# Ok(())
# }