PHP Futures UserData

Hello.

How to change PHP code below to start working with FUTURES WEBSOCKET @userdata. It works only on SPOT.

Is correct v1/userDataStream?
Is correct v1/listenKey={$listenKey}?
Is correct “balanceCallback” and “executionCallback”?

Thank you

PHP code:

/**
 * userData Issues userDataStream token and keepalive, subscribes to userData WebSocket
 *
 * $balance_update = function($api, $balances) {
 * print_r($balances);
 * echo "Balance update".PHP_EOL;
 * };
 *
 * $order_update = function($api, $report) {
 * echo "Order update".PHP_EOL;
 * print_r($report);
 * $price = $report['price'];
 * $quantity = $report['quantity'];
 * $symbol = $report['symbol'];
 * $side = $report['side'];
 * $orderType = $report['orderType'];
 * $orderId = $report['orderId'];
 * $orderStatus = $report['orderStatus'];
 * $executionType = $report['orderStatus'];
 * if( $executionType == "NEW" ) {
 * if( $executionType == "REJECTED" ) {
 * echo "Order Failed! Reason: {$report['rejectReason']}".PHP_EOL;
 * }
 * echo "{$symbol} {$side} {$orderType} ORDER #{$orderId} ({$orderStatus})".PHP_EOL;
 * echo "..price: {$price}, quantity: {$quantity}".PHP_EOL;
 * return;
 * }
 *
 * //NEW, CANCELED, REPLACED, REJECTED, TRADE, EXPIRED
 * echo "{$symbol} {$side} {$executionType} {$orderType} ORDER #{$orderId}".PHP_EOL;
 * };
 * $api->userData($balance_update, $order_update);
 *
 * @param $balance_callback callable function
 * @param bool $execution_callback callable function
 * @return null
 * @throws \Exception
 */
public function userData(&$balance_callback, &$execution_callback = false)
{
    $response = $this->httpRequest("v1/userDataStream", "POST", []);
    $this->listenKey = $response['listenKey'];

    $this->info['balanceCallback'] = $balance_callback;
    $this->info['executionCallback'] = $execution_callback;

    $this->subscriptions['@userdata'] = true;

    $loop = \React\EventLoop\Factory::create();
    $loop->addPeriodicTimer(30*60, function () {
        $listenKey = $this->listenKey;
        $this->httpRequest("v1/listenKey={$listenKey}", "PUT", []);
    });
    $connector = new \Ratchet\Client\Connector($loop);

    // @codeCoverageIgnoreStart
    // phpunit can't cover async function
    $connector($this->getWsEndpoint() . $this->listenKey)->then(function ($ws) {
        $ws->on('message', function ($data) use ($ws) {
            if ($this->subscriptions['@userdata'] === false) {
                //$this->subscriptions[$endpoint] = null;
                $ws->close();
                return; //return $ws->close();
            }
            $json = json_decode($data);
            $type = $json->e;
            if ($type === "outboundAccountPosition") {
                $balances = $this->balanceHandler($json->B);
                $this->info['balanceCallback']($this, $balances);
            } elseif ($type === "executionReport") {
                $report = $this->executionHandler($json);
                if ($this->info['executionCallback']) {
                    $this->info['executionCallback']($this, $report);
                }
            }
        });
        $ws->on('close', function ($code = null, $reason = null) {
            // WPCS: XSS OK.
            echo "userData: WebSocket Connection closed! ({$code} - {$reason})" . PHP_EOL;
        });
    }, function ($e) {
        // WPCS: XSS OK.
        echo "userData: Could not connect: {$e->getMessage()}" . PHP_EOL;
    });

    $loop->run();
}
  • Futures websocket and rest base url are difference from SPOT.
  • Futures does not share the same event names as SPOT (such as executionReport and outboundAccountPosition).

Spot user data stream documentation
https://binance-docs.github.io/apidocs/spot/en/#user-data-streams

Futures user data stream documentation
https://binance-docs.github.io/apidocs/futures/en/#user-data-streams

Now It works.

Thank you!