pub struct OkxWs { /* private fields */ }Expand description
OKX WebSocket client.
Provides real-time data streaming for OKX exchange.
Implementations§
Source§impl OkxWs
impl OkxWs
Sourcepub async fn disconnect(&self) -> Result<()>
pub async fn disconnect(&self) -> Result<()>
Disconnects from the WebSocket server.
Sourcepub fn state(&self) -> WsConnectionState
pub fn state(&self) -> WsConnectionState
Returns the current connection state.
Sourcepub fn is_connected(&self) -> bool
pub fn is_connected(&self) -> bool
Checks if the WebSocket is connected.
Sourcepub async fn subscribe_ticker(&self, symbol: &str) -> Result<()>
pub async fn subscribe_ticker(&self, symbol: &str) -> Result<()>
Subscribes to a ticker stream.
§Arguments
symbol- Trading pair symbol in OKX format (e.g., “BTC-USDT”)
Sourcepub async fn subscribe_tickers(&self, symbols: &[String]) -> Result<()>
pub async fn subscribe_tickers(&self, symbols: &[String]) -> Result<()>
Subscribes to multiple ticker streams.
§Arguments
symbols- List of trading pair symbols (e.g.,["BTC-USDT", "ETH-USDT"])
Sourcepub async fn watch_tickers(
&self,
symbols: &[String],
) -> Result<MessageStream<Vec<Ticker>>>
pub async fn watch_tickers( &self, symbols: &[String], ) -> Result<MessageStream<Vec<Ticker>>>
Sourcepub async fn subscribe_orderbook(&self, symbol: &str, depth: u32) -> Result<()>
pub async fn subscribe_orderbook(&self, symbol: &str, depth: u32) -> Result<()>
depth- Orderbook depth (5, 50, or 400)
Sourcepub async fn subscribe_trades(&self, symbol: &str) -> Result<()>
pub async fn subscribe_trades(&self, symbol: &str) -> Result<()>
Subscribes to a trades stream.
§Arguments
symbol- Trading pair symbol in OKX format (e.g., “BTC-USDT”)
Sourcepub async fn subscribe_kline(&self, symbol: &str, interval: &str) -> Result<()>
pub async fn subscribe_kline(&self, symbol: &str, interval: &str) -> Result<()>
Subscribes to a kline/candlestick stream.
§Arguments
symbol- Trading pair symbol in OKX format (e.g., “BTC-USDT”)interval- Kline interval (e.g., “1m”, “5m”, “1H”, “1D”)
Sourcepub async fn unsubscribe(&self, stream_name: String) -> Result<()>
pub async fn unsubscribe(&self, stream_name: String) -> Result<()>
Sourcepub async fn subscriptions(&self) -> Vec<String>
pub async fn subscriptions(&self) -> Vec<String>
Returns the list of active subscriptions.
Sourcepub async fn watch_ticker(
&self,
symbol: &str,
market: Option<Market>,
) -> Result<MessageStream<Ticker>>
pub async fn watch_ticker( &self, symbol: &str, market: Option<Market>, ) -> Result<MessageStream<Ticker>>
Sourcepub async fn watch_order_book(
&self,
symbol: &str,
limit: Option<u32>,
) -> Result<MessageStream<OrderBook>>
pub async fn watch_order_book( &self, symbol: &str, limit: Option<u32>, ) -> Result<MessageStream<OrderBook>>
Sourcepub async fn watch_trades(
&self,
symbol: &str,
market: Option<Market>,
) -> Result<MessageStream<Vec<Trade>>>
pub async fn watch_trades( &self, symbol: &str, market: Option<Market>, ) -> Result<MessageStream<Vec<Trade>>>
Sourcepub async fn login(&self, auth: &OkxAuth) -> Result<()>
pub async fn login(&self, auth: &OkxAuth) -> Result<()>
Sends a login message to authenticate the private WebSocket connection.
OKX WebSocket login format:
{
"op": "login",
"args": [{
"apiKey": "...",
"passphrase": "...",
"timestamp": "...",
"sign": "..."
}]
}The signature is: HMAC-SHA256(timestamp + “GET” + “/users/self/verify”, secret)
Sourcepub async fn subscribe_orders(&self, inst_type: Option<&str>) -> Result<()>
pub async fn subscribe_orders(&self, inst_type: Option<&str>) -> Result<()>
Subscribes to the orders channel (private).
Receives real-time order updates for all instrument types.
Sourcepub async fn subscribe_account(&self) -> Result<()>
pub async fn subscribe_account(&self) -> Result<()>
Subscribes to the account channel (private).
Receives real-time balance/account updates.
Sourcepub async fn watch_balance(&self) -> Result<MessageStream<Balance>>
pub async fn watch_balance(&self) -> Result<MessageStream<Balance>>
Watches balance updates via the private WebSocket.
Requires prior authentication via login().
Sourcepub async fn watch_orders(
&self,
symbol_filter: Option<String>,
) -> Result<MessageStream<Order>>
pub async fn watch_orders( &self, symbol_filter: Option<String>, ) -> Result<MessageStream<Order>>
Watches order updates via the private WebSocket.
Requires prior authentication via login().
Sourcepub async fn watch_my_trades(
&self,
symbol_filter: Option<String>,
) -> Result<MessageStream<Trade>>
pub async fn watch_my_trades( &self, symbol_filter: Option<String>, ) -> Result<MessageStream<Trade>>
Watches trade fills via the private WebSocket (orders channel).
Extracts trade information from order execution reports.
Requires prior authentication via login().