Binance Websocket Ping Pong

Are you ready to dive into the world of Binance Websocket Ping Pong? In this article, we will explore the essential details and guidelines for using the Binance Websocket Ping Pong feature effectively. Whether you are a beginner or an experienced trader, this article will provide you with all the information you need to know. So, let’s get started!

Websocket Endpoints

To begin with, you need to connect to the Binance Websocket server through the following endpoints:

  • wss://stream.binance.com:9443 or wss://stream.binance.com:443

These endpoints support both single raw streams and combined streams. If you prefer raw streams, you can access them using the /ws/<streamName> endpoint. On the other hand, if you want to access combined streams, you can use the /stream?streams=<streamName1>/<streamName2>/<streamName3> endpoint. It’s important to note that all symbols for streams are in lowercase.

Connection and Ping Pong

When connecting to the Websocket server, it’s essential to keep a few things in mind. First, a single connection to stream.binance.com is valid for 24 hours. After the 24-hour mark, you should expect to be disconnected. To maintain the connection, the Websocket server will send a ping frame every 3 minutes. It’s crucial to respond with a pong frame containing a copy of the ping’s payload as soon as possible. Failure to send the pong frame within a 10-minute period will result in disconnection. Additionally, unsolicited pong frames are allowed but won’t prevent disconnection. It’s recommended to keep the payload for these pong frames empty.

Market Data and Limitations

If you are interested in receiving only market data messages without user data stream, you can subscribe to the base endpoint wss://data-stream.binance.vision. However, it’s important to note that this endpoint doesn’t provide user data stream functionality.

There are also certain limitations to consider when working with Binance Websocket Ping Pong. WebSocket connections have a limit of 5 incoming messages per second, which include PING frames, PONG frames, and JSON controlled messages (e.g., subscribe, unsubscribe). If a connection exceeds this limit, it will be disconnected, and repeated disconnections may lead to IP banning. Furthermore, a single connection can only listen to a maximum of 1024 streams, and there’s a limit of 300 connections per attempt every 5 minutes per IP.

Subscribing and Unsubscribing to Streams

To subscribe or unsubscribe from streams, you can send the following data through the Websocket instance:

  • id: An identifier to uniquely identify the messages going back and forth. It can be a 64-bit signed integer, an alphanumeric string (max length 36), or null.

In the response, if the result received is null, it indicates that the non-query request (e.g., subscribing/unsubscribing) was successful.

Handling Error Messages

When working with Binance Websocket Ping Pong, it’s essential to be aware of possible error messages. Here are some examples of error messages you may encounter:

  • Unknown property: Indicates that the parameter used in the SET_PROPERTY or GET_PROPERTY request was invalid.
  • Invalid value type: expected Boolean: Indicates that the value should only be either true or false.
  • Invalid request: property name must be a string: Indicates that the provided property name was invalid.
  • Invalid request: request ID must be an unsigned integer: Indicates that the id parameter was not provided or the value provided is unsupported.
  • Invalid request: unknown variant: Indicates a possible typo in the provided method or an unexpected method value.

Stream Types

Now, let’s explore the different stream types available in Binance Websocket Ping Pong and their corresponding payloads:

Aggregate Trade Streams

Trade Streams

Kline/Candlestick Streams for UTC

Kline/Candlestick Streams with Timezone Offset

Individual Symbol Mini Ticker Stream

All Market Mini Tickers Stream

Individual Symbol Ticker Streams

All Market Tickers Stream

Individual Symbol Rolling Window Statistics Streams

All Market Rolling Window Statistics Streams

Individual Symbol Book Ticker Streams

Average Price

Partial Book Depth Streams

  • Stream Names: <symbol>@depth<levels> or <symbol>@depth<levels>@100ms
  • Valid <levels>: 5, 10, or 20
  • Update Speed: 1000ms or 100ms
  • Payload: Click here to view the payload

Diff. Depth Stream

Managing a Local Order Book Correctly

If you want to manage a local order book correctly, follow these steps:

  1. Open a stream to wss://stream.binance.com:9443/ws/bnbbtc@depth.
  2. Buffer the events received from the stream.
  3. Get a depth snapshot from https://api.binance.com/api/v3/depth?symbol=BNBBTC&limit=1000.
  4. Drop any event where u is less than or equal to lastUpdateId in the snapshot.
  5. The first processed event should have U less than or equal to lastUpdateId+1 and u greater than or equal to lastUpdateId+1.
  6. While listening to the stream, ensure that each new event’s U is equal to the previous event’s u+1.
  7. The data in each event represents the absolute quantity for a price level.
  8. If the quantity is 0, remove the price level.
  9. It’s normal to receive an event that removes a price level not in your local order book.

It’s important to note that due to the depth snapshot’s limit on price levels, price levels outside the initial snapshot without quantity changes won’t have an update in the Diff. Depth Stream. As a result, those price levels won’t be visible in the local order book, causing slight differences between the local and real order books. However, for most use cases, the depth limit of 5000 provides sufficient market understanding for effective trading.

Congratulations! You are now equipped with the knowledge to make the most of Binance Websocket Ping Pong. Start exploring the various stream types and unleash your trading potential. Happy trading!

Binance Websocket