Skip to content

M3 #29: Rust SDK: WebSocket client for real-time streaming#69

Merged
joaquinbejar merged 1 commit into
mainfrom
M3/issue-29-websocket-client
Feb 1, 2026
Merged

M3 #29: Rust SDK: WebSocket client for real-time streaming#69
joaquinbejar merged 1 commit into
mainfrom
M3/issue-29-websocket-client

Conversation

@joaquinbejar

Copy link
Copy Markdown
Owner

Summary

Implements the WebSocket client that connects to the streaming API for real-time order book updates, trades, and user order notifications.

Changes

New Module: sdk/src/ws/

File Description
mod.rs Module exports
config.rs WebSocket configuration
error.rs WebSocket-specific errors
messages.rs Message types for client/server communication
client.rs WebSocket client implementation

WebSocket Configuration

  • URL: Configurable WebSocket endpoint
  • Heartbeat Interval: Ping interval (default: 30s)
  • Reconnect Delay: Initial reconnect delay (default: 1s)
  • Max Reconnect Delay: Maximum backoff (default: 30s)
  • API Key: Optional authentication

Subscription Management

  • subscribe_book(market, depth) — Order book updates
  • subscribe_trades(market) — Trade stream
  • subscribe_orders() — User order updates (authenticated)
  • subscribe_ticker(market) — Price ticker
  • unsubscribe(channel, market) — Unsubscribe from channel

Message Types

Client Messages:

  • Subscribe — Subscribe to a channel
  • Unsubscribe — Unsubscribe from a channel
  • Ping — Heartbeat ping

Server Messages:

  • Subscribed / Unsubscribed — Subscription confirmations
  • BookSnapshot — Full order book snapshot
  • BookUpdate — Incremental book updates (deltas)
  • Trade — Trade events
  • Ticker — Price ticker updates
  • OrderUpdate — User order updates
  • Pong — Heartbeat response
  • Error — Error messages

Connection Lifecycle

  • Automatic heartbeat/ping-pong handling
  • Event stream via next_event()
  • Subscription tracking
  • Graceful shutdown via close()

Dependencies

  • Add tokio-tungstenite v0.26 with rustls-tls
  • Add futures-util v0.3

Technical Decisions

  • tokio-tungstenite: Industry-standard async WebSocket library
  • Async stream API: Events delivered via next_event() method
  • Thread-safe subscriptions: Using RwLock<HashSet<Subscription>>
  • Automatic heartbeat: Spawns background task for ping/pong
  • Message parsing: Deserializes into SDK types

Testing

  • 28 unit tests for WebSocket client
  • Configuration validation tests
  • Message serialization/deserialization tests
  • Subscription tracking tests

Checklist

  • Code follows .internalDoc/09-rust-guidelines.md
  • Documentation added for all public items
  • No warnings from cargo clippy
  • cargo fmt passes
  • All tests pass

Closes #29

- Add ws module to SDK with WebSocket client for real-time streaming:
  - config.rs: WsConfig with URL, heartbeat, reconnection settings
  - error.rs: WsError for connection, protocol, subscription errors
  - messages.rs: Message types for client/server communication
  - client.rs: MatchbookWsClient with async methods
- Subscription management:
  - subscribe_book(market, depth) — Order book updates
  - subscribe_trades(market) — Trade stream
  - subscribe_orders() — User order updates (authenticated)
  - subscribe_ticker(market) — Price ticker
  - unsubscribe(channel, market) — Unsubscribe from channel
- Message types:
  - ClientMessage: Subscribe, Unsubscribe, Ping
  - ServerMessage: Subscribed, BookSnapshot, BookUpdate, Trade, Ticker, OrderUpdate, Pong, Error
  - Channel: Book, Trades, Orders, Ticker
  - Subscription: Track active subscriptions
- Connection lifecycle:
  - Automatic heartbeat/ping-pong
  - Event stream via next_event()
  - Graceful shutdown via close()
- Add tokio-tungstenite and futures-util to workspace dependencies
- 28 unit tests for WebSocket client
@joaquinbejar joaquinbejar added this to the M3: Client SDKs milestone Feb 1, 2026
@joaquinbejar joaquinbejar merged commit 4018908 into main Feb 1, 2026
5 checks passed
@joaquinbejar joaquinbejar deleted the M3/issue-29-websocket-client branch February 2, 2026 09:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Rust SDK: WebSocket client for real-time streaming

1 participant