Module websocket

Module websocket 

Source
Expand description

Python bindings for the Coinbase Intx WebSocket client.

§Design Pattern: Clone and Share State

The WebSocket client must be cloned for async operations because PyO3’s future_into_py requires 'static futures (cannot borrow from self). To ensure clones share the same connection state, key fields use Arc<RwLock<T>>:

  • inner: Arc<RwLock<Option<WebSocketClient>>> - The WebSocket connection.

Without shared state, clones would be independent, causing:

  • Lost WebSocket messages.
  • Missing instrument data.
  • Connection state desynchronization.

§Connection Flow

  1. Clone the client for async operation.
  2. Connect and populate shared state on the clone.
  3. Spawn stream handler as background task.
  4. Return immediately (non-blocking).

§Important Notes

  • Never use block_on() - it blocks the runtime.
  • Always clone before async blocks for lifetime requirements.
  • RwLock is preferred over Mutex (many reads, few writes).

Functions§

call_python