kalshi-cpp 0.6.2
C++23 client for Kalshi's Predictions API
Loading...
Searching...
No Matches
kalshi::WebSocketClient Class Reference

Streams Kalshi's WebSocket channels. More...

#include <kalshi/websocket.hpp>

Public Member Functions

 WebSocketClient (Signer signer, WsConfig config={})
 Creates a client that authenticates with a copy of signer.
 
 ~WebSocketClient ()
 
 WebSocketClient (WebSocketClient &&) noexcept
 
WebSocketClient & operator= (WebSocketClient &&) noexcept
 
 WebSocketClient (const WebSocketClient &)=delete
 
WebSocketClient & operator= (const WebSocketClient &)=delete
 
Result< void > connect ()
 Opens the connection and waits up to connect_timeout for the handshake.
 
void disconnect ()
 Closes the connection and forgets every subscription.
 
WsState state () const noexcept
 
bool is_connected () const noexcept
 
Result< ws::Subscription > subscribe (ws::Channel channel, ws::SubscribeParams params={})
 Subscribes to one channel.
 
Result< void > unsubscribe (ws::Subscription subscription)
 
Result< void > update_subscription (ws::Subscription subscription, const ws::UpdateSubscriptionParams &params)
 
Result< void > add_markets (ws::Subscription subscription, std::vector< std::string > market_tickers)
 
Result< void > remove_markets (ws::Subscription subscription, std::vector< std::string > market_tickers)
 
Result< void > request_snapshot (ws::Subscription subscription, std::vector< std::string > market_tickers)
 Asks for order book snapshots of markets in an orderbook_delta subscription, without changing it.
 
Result< std::int64_t > list_subscriptions ()
 Asks the server for its view of this connection's subscriptions.
 
std::vector< ws::Subscription > subscriptions () const
 The subscriptions this client holds.
 
void on_message (std::function< void(const WsMessage &)> callback)
 
void on_error (std::function< void(const WsError &)> callback)
 
void on_state_change (std::function< void(WsState)> callback)
 
const WsConfig & config () const noexcept
 

Detailed Description

Streams Kalshi's WebSocket channels.

Subscriptions survive reconnects: the client resubscribes with the same parameters and keeps each ws::Subscription handle, while the server's sid changes. Callbacks run on the client's network thread, which blocks SIGPIPE on POSIX systems, except the Disconnected state change from disconnect(), which runs on the caller's. They may call any method, including destroying the client, but should return quickly. Every method is thread-safe.

Constructor & Destructor Documentation

◆ WebSocketClient() [1/3]

kalshi::WebSocketClient::WebSocketClient ( Signer  signer,
WsConfig  config = {} 
)
explicit

Creates a client that authenticates with a copy of signer.

◆ ~WebSocketClient()

kalshi::WebSocketClient::~WebSocketClient ( )

◆ WebSocketClient() [2/3]

kalshi::WebSocketClient::WebSocketClient ( WebSocketClient &&  )
noexcept

◆ WebSocketClient() [3/3]

kalshi::WebSocketClient::WebSocketClient ( const WebSocketClient &  )
delete

Member Function Documentation

◆ operator=() [1/2]

WebSocketClient & kalshi::WebSocketClient::operator= ( WebSocketClient &&  )
noexcept

◆ operator=() [2/2]

WebSocketClient & kalshi::WebSocketClient::operator= ( const WebSocketClient &  )
delete

◆ connect()

Result< void > kalshi::WebSocketClient::connect ( )

Opens the connection and waits up to connect_timeout for the handshake.

Subscriptions made before the call are sent once connected.

◆ disconnect()

void kalshi::WebSocketClient::disconnect ( )

Closes the connection and forgets every subscription.

connect() can open a new session afterwards.

◆ state()

WsState kalshi::WebSocketClient::state ( ) const
noexcept

◆ is_connected()

bool kalshi::WebSocketClient::is_connected ( ) const
noexcept

◆ subscribe()

Result< ws::Subscription > kalshi::WebSocketClient::subscribe ( ws::Channel  channel,
ws::SubscribeParams  params = {} 
)

Subscribes to one channel.

The handle is usable at once: updates and unsubscribes made before the server confirms are sent after it does.

◆ unsubscribe()

Result< void > kalshi::WebSocketClient::unsubscribe ( ws::Subscription  subscription)

◆ update_subscription()

Result< void > kalshi::WebSocketClient::update_subscription ( ws::Subscription  subscription,
const ws::UpdateSubscriptionParams &  params 
)

◆ add_markets()

Result< void > kalshi::WebSocketClient::add_markets ( ws::Subscription  subscription,
std::vector< std::string >  market_tickers 
)

◆ remove_markets()

Result< void > kalshi::WebSocketClient::remove_markets ( ws::Subscription  subscription,
std::vector< std::string >  market_tickers 
)

◆ request_snapshot()

Result< void > kalshi::WebSocketClient::request_snapshot ( ws::Subscription  subscription,
std::vector< std::string >  market_tickers 
)

Asks for order book snapshots of markets in an orderbook_delta subscription, without changing it.

◆ list_subscriptions()

Result< std::int64_t > kalshi::WebSocketClient::list_subscriptions ( )

Asks the server for its view of this connection's subscriptions.

The reply arrives as ws::SubscriptionList carrying the returned ID.

◆ subscriptions()

std::vector< ws::Subscription > kalshi::WebSocketClient::subscriptions ( ) const

The subscriptions this client holds.

◆ on_message()

void kalshi::WebSocketClient::on_message ( std::function< void(const WsMessage &)>  callback)

◆ on_error()

void kalshi::WebSocketClient::on_error ( std::function< void(const WsError &)>  callback)

◆ on_state_change()

void kalshi::WebSocketClient::on_state_change ( std::function< void(WsState)>  callback)

◆ config()

const WsConfig & kalshi::WebSocketClient::config ( ) const
noexcept

The documentation for this class was generated from the following file: