Binance offers a variety of channels where you can subscribe to get real-time updates of market data, orders... Find below a sample of how to subscribe to a Ticker:
oClient := TsgcWebSocketClient.Create(nil);
oBinance := TsgcWSAPI_Binance.Create(nil);
oBinance.Client := oClient;
oBinance.SubscribeTicker('bnbbtc');
procedure OnMessage(Connection: TsgcWSConnection; const aText: string);
begin
// here you will receive the ticker updates
end;
Binance closes a connection that sends more than 5 messages per second, so subscribing to a watchlist of several symbols by calling the individual Subscribe methods in a loop can be enough to lose the connection. Call SubscribeStreams with an array of stream names and the client sends a single frame instead of one frame per stream. UnsubscribeStreams works the same way. Both methods return the identifier of the message sent to the server.
oBinance.SubscribeStreams(['btcusdt@aggTrade', 'btcusdt@depth', 'ethusdt@aggTrade']);
// and to unsubscribe
oBinance.UnsubscribeStreams(['btcusdt@aggTrade', 'btcusdt@depth']);
If the application still needs to send many messages in a short time, enable the Throttle option of the API client, which limits how many messages the client sends inside a time window. Read WebSocket APIs for the details.