brainos-bridge 0.5.0

WebSocket relay client for connecting external platforms to Brain OS
Documentation

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

  1. [BridgeClient] connects to the configured url via WebSocket.
  2. 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.
  3. WebSocket Ping frames are answered with Pong automatically. Close frames trigger the reconnect loop after the configured backoff.
  4. A caller-supplied handler is invoked for each parsed message and must return a [BridgeMessage] response (the response keeps the original message's id if built via [BridgeMessage::reply]).
  5. 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.
  6. On disconnect, the client waits initial_backoff_ms * 2^attempt (capped at max_backoff_ms) before retrying. With [BridgeConfig::max_reconnect_attempts] set, the loop returns [BridgeError::MaxRetriesExceeded] once the cap is reached; with None, 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(())
# }