I am trying to connect to user data stream in binance api futures, but I get 504 Gateway Time Out error. It breaks where I dump $connector variable in my code. Here is my function where it breaks.
public function userData(&$balance_callback, &$execution_callback = false)
{
$response = $this->httpRequest("v1/listenKey", "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={$listenKey}", "PUT", []);
});
$connector = new \Ratchet\Client\Connector($loop);
// @codeCoverageIgnoreStart
// phpunit can't cover async function
$connector($this->getWsEndpoint() . $this->listenKey)->then(function ($ws) {
var_dump($connector); // HERE IT BREAKS
$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();
}
private function getWsEndpoint() : string
{
return $this->stream;
}
and $stream variable value is
protected $stream = 'wss://fstream.binance.com';
In my controller I call userData function
$api = new Binance\API($key, $secret);
$api->userData($call1, $call2);
I am using php 7 and jaggedsoft/php-binance-api package for this.
There is no error message, it just takes long to open request and then it shows ‘504 Gateway Time-out’ error.
I am having trouble installing websocat, but when I try here https://www.piesocket.com/websocket-tester it establishes connection, but it probably doesn’t mean anything.
If there is no problem connecting to websocket server from your side, there may be an issue in your code.
Here is a PHP library is written in PHP but only for spot, I suggest checking how it implements the websocket part, which may help your futures implementation.
Futures API is quite similar to spot, easy to understand both if you figure out how it works on spot side.