Skip to content

WebSocket API

Igor Sazonov edited this page Jan 31, 2026 · 1 revision

WebSocket API

The bingx-php SDK provides support for the BingX WebSocket API, allowing you to subscribe to real-time data streams for market data and account updates. This is essential for building applications that require low-latency information.

Listen Key

To subscribe to private data streams (such as account updates), you first need to obtain a listen key. The ListenKeyService is used for this purpose.

Method: createListenKey()

$listenKeyData = Bingx::listenKey()->createListenKey();
$listenKey = $listenKeyData["listenKey"];

You should periodically extend the validity of your listen key to keep the connection alive.

Method: extendListenKey()

Bingx::listenKey()->extendListenKey($listenKey);

Market Data Streams

The MarketDataStream class provides methods for subscribing to public market data streams.

use Tigusigalpa\BingX\WebSocket\MarketDataStream;

$marketStream = new MarketDataStream();

// Subscribe to the ticker stream for BTC-USDT
$marketStream->subscribeToTicker("BTC-USDT", function ($data) {
    echo "Ticker Update: " . json_encode($data) . "\n";
});

// Subscribe to the depth stream
$marketStream->subscribeToDepth("BTC-USDT", function ($data) {
    echo "Depth Update: " . json_encode($data) . "\n";
});

$marketStream->connect();

Account Data Streams

The AccountDataStream class is used for subscribing to private account data streams.

use Tigusigalpa\BingX\WebSocket\AccountDataStream;

$listenKey = Bingx::listenKey()->createListenKey()["listenKey"];

$accountStream = new AccountDataStream($listenKey);

// Subscribe to account balance and position updates
$accountStream->subscribeToAccountUpdates(function ($data) {
    echo "Account Update: " . json_encode($data) . "\n";
});

// Subscribe to order updates
$accountStream->subscribeToOrderUpdates(function ($data) {
    echo "Order Update: " . json_encode($data) . "\n";
});

$accountStream->connect();

Handling WebSocket Connections

The connect() method starts the WebSocket connection and enters a loop to listen for incoming messages. You should run this in a long-running process, such as a command-line script or daemon-based application.

Clone this wiki locally