Binance C++ Connector 2.0.0
Official C++ connectors for the Binance APIs
Loading...
Searching...
No Matches
binance::common::WebsocketStreams Class Reference

The WebSocket market / user streams transport shared by every product. More...

#include <websocket_streams.hpp>

Public Types

using ConnectSignature = void(std::exception_ptr)

Public Member Functions

 WebsocketStreams (WebsocketStreamsConfig configuration, std::vector< std::string > url_paths={}, detail::ClientOptions options={})
 Throws ConfigError if url or proxy is invalid.
 ~WebsocketStreams ()
 WebsocketStreams (const WebsocketStreams &)=delete
WebsocketStreams & operator= (const WebsocketStreams &)=delete
 WebsocketStreams (WebsocketStreams &&)=delete
WebsocketStreams & operator= (WebsocketStreams &&)=delete
const WebsocketStreamsConfig & configuration () const noexcept
 The configuration, as given but without private_key_passphrase (the key is loaded).
boost::asio::any_io_executor get_executor () const noexcept
template<class CompletionToken>
auto async_connect (std::vector< std::string > streams, CompletionToken &&token)
 Opens every connection that is not open.
void connect (std::vector< std::string > streams={})
template<class CompletionToken>
auto async_disconnect (CompletionToken &&token)
 Drops every subscription and callback (existing handles no longer do anything), then closes the connections like WebsocketApi::disconnect.
void disconnect ()
bool is_connected () const
void ping_server ()
template<class F>
Subscription on_event (F &&callback, bool with_messages=true)
void subscribe (const std::vector< std::string > &streams, std::optional< StreamId > id={}, const std::optional< std::string > &url_path={})
 Sends SUBSCRIBE for the streams not already subscribed, or queues them until their connection opens.
void unsubscribe (const std::vector< std::string > &streams, std::optional< StreamId > id={}, const std::optional< std::string > &url_path={})
 Sends UNSUBSCRIBE for the streams that no handle uses.
bool is_subscribed (const std::string &stream) const
 True if stream is subscribed (or queued), on any URL path.
WebsocketStreamHandle create_stream (std::string stream, std::optional< StreamId > id={}, std::optional< std::string > url_path={})
 Subscribes to stream and returns a handle for its callbacks.
template<class CompletionToken>
auto async_wait_subscribed (std::string stream, std::optional< std::string > url_path, CompletionToken &&token)
 Completes once the server has acknowledged the subscription of stream (a SUBSCRIBE response, or a connection that opened with it in its URL), at once if it already has.
void wait_subscribed (std::string stream, std::optional< std::string > url_path={}, std::optional< std::chrono::milliseconds > timeout={})

Static Public Member Functions

static std::string stream_key (const std::string &stream, const std::optional< std::string > &url_path)
 url_path::stream, or stream without a URL path.

Detailed Description

The WebSocket market / user streams transport shared by every product.

  • A pool of mode.pool_size() connections per URL path (e.g. market, public, private for USDⓈ-M futures, or the base URL alone). Streams are spread over it round-robin, at most 1024 per connection while another connection has room.
  • Subscriptions made while no connection is open (reconnecting, or between disconnect() and the next connect()) are kept, and sent once their connection opens.
  • SUBSCRIBE and UNSUBSCRIBE made in one go (e.g. a loop of create_stream) are batched into one request per connection when they are still queued (always, when made on the client's executor). Nothing is delayed for it.
  • Reconnects and 23 h renewals resubscribe through the connection URL; streams that do not fit in it (about 4 KB) are subscribed once the connection opens. Signed streams have no streams in their URL: they are all subscribed again by command once the connection opens.
  • Around a renewal, a stream comes from the previous socket until the new one delivers it, so a few messages may repeat.
  • A SUBSCRIBE the server rejects drops its streams (is_subscribed turns false, they leave reconnect URLs) and emits an Error event. Signed streams' commands have no id: each acknowledgement answers the oldest command of its kind sent on that socket.
  • Stream callbacks are keyed by stream_key(stream, url_path). UNSUBSCRIBE is sent only once no handle uses a stream.
  • Streams of a connection that closes for good move to another connection of the pool, or stay until the next connect() if none is left.

Threading, callbacks and destruction work as for WebsocketApi. Only connect, disconnect and wait_subscribed wait for the executor (they throw if called on it).

Member Typedef Documentation

◆ ConnectSignature

using binance::common::WebsocketStreams::ConnectSignature = void(std::exception_ptr)

Constructor & Destructor Documentation

◆ WebsocketStreams() [1/3]

binance::common::WebsocketStreams::WebsocketStreams ( WebsocketStreamsConfig configuration,
std::vector< std::string > url_paths = {},
detail::ClientOptions options = {} )
explicit

Throws ConfigError if url or proxy is invalid.

◆ ~WebsocketStreams()

binance::common::WebsocketStreams::~WebsocketStreams ( )

◆ WebsocketStreams() [2/3]

binance::common::WebsocketStreams::WebsocketStreams ( const WebsocketStreams & )
delete

◆ WebsocketStreams() [3/3]

binance::common::WebsocketStreams::WebsocketStreams ( WebsocketStreams && )
delete

Member Function Documentation

◆ operator=() [1/2]

WebsocketStreams & binance::common::WebsocketStreams::operator= ( const WebsocketStreams & )
delete

◆ operator=() [2/2]

WebsocketStreams & binance::common::WebsocketStreams::operator= ( WebsocketStreams && )
delete

◆ configuration()

const WebsocketStreamsConfig & binance::common::WebsocketStreams::configuration ( ) const
nodiscardnoexcept

The configuration, as given but without private_key_passphrase (the key is loaded).

◆ get_executor()

boost::asio::any_io_executor binance::common::WebsocketStreams::get_executor ( ) const
nodiscardnoexcept

◆ async_connect()

template<class CompletionToken>
auto binance::common::WebsocketStreams::async_connect ( std::vector< std::string > streams,
CompletionToken && token )
inline

Opens every connection that is not open.

streams are subscribed through the URL of the first connection of each URL path, and survive reconnects like other subscriptions.

◆ connect()

void binance::common::WebsocketStreams::connect ( std::vector< std::string > streams = {})

◆ async_disconnect()

template<class CompletionToken>
auto binance::common::WebsocketStreams::async_disconnect ( CompletionToken && token)
inline

Drops every subscription and callback (existing handles no longer do anything), then closes the connections like WebsocketApi::disconnect.

Subscriptions made afterwards are sent by the next connect().

◆ disconnect()

void binance::common::WebsocketStreams::disconnect ( )

◆ is_connected()

bool binance::common::WebsocketStreams::is_connected ( ) const
nodiscard

◆ ping_server()

void binance::common::WebsocketStreams::ping_server ( )

◆ on_event()

template<class F>
Subscription binance::common::WebsocketStreams::on_event ( F && callback,
bool with_messages = true )
inline

◆ subscribe()

void binance::common::WebsocketStreams::subscribe ( const std::vector< std::string > & streams,
std::optional< StreamId > id = {},
const std::optional< std::string > & url_path = {} )

Sends SUBSCRIBE for the streams not already subscribed, or queues them until their connection opens.

id is normalised with normalize_stream_id. Throws WebsocketError for an empty stream name or a URL path the client was not created with.

◆ unsubscribe()

void binance::common::WebsocketStreams::unsubscribe ( const std::vector< std::string > & streams,
std::optional< StreamId > id = {},
const std::optional< std::string > & url_path = {} )

Sends UNSUBSCRIBE for the streams that no handle uses.

◆ is_subscribed()

bool binance::common::WebsocketStreams::is_subscribed ( const std::string & stream) const
nodiscard

True if stream is subscribed (or queued), on any URL path.

Reflects subscribe, unsubscribe, create_stream, handle unsubscribes and disconnect as soon as they return, and a rejected SUBSCRIBE once its response arrives.

◆ create_stream()

WebsocketStreamHandle binance::common::WebsocketStreams::create_stream ( std::string stream,
std::optional< StreamId > id = {},
std::optional< std::string > url_path = {} )
nodiscard

Subscribes to stream and returns a handle for its callbacks.

Data arriving before the first callback is added is kept for it (up to 1000 messages). Throws like subscribe.

◆ async_wait_subscribed()

template<class CompletionToken>
auto binance::common::WebsocketStreams::async_wait_subscribed ( std::string stream,
std::optional< std::string > url_path,
CompletionToken && token )
inline

Completes once the server has acknowledged the subscription of stream (a SUBSCRIBE response, or a connection that opened with it in its URL), at once if it already has.

It waits across reconnects and while the client is not connected yet. Fails with WebsocketResponseError if the server rejected the SUBSCRIBE, and with WebsocketCancelledError if the stream is not subscribed, or stops being before (unsubscribed, disconnect(), destruction of the client), or if the token's cancellation slot fires (any cancellation type; the subscription stays). wait_subscribed blocks and throws WebsocketTimeoutError once timeout passes, if given.

◆ wait_subscribed()

void binance::common::WebsocketStreams::wait_subscribed ( std::string stream,
std::optional< std::string > url_path = {},
std::optional< std::chrono::milliseconds > timeout = {} )

◆ stream_key()

std::string binance::common::WebsocketStreams::stream_key ( const std::string & stream,
const std::optional< std::string > & url_path )
staticnodiscard

url_path::stream, or stream without a URL path.