Sockets functionality for v2
Hello team,
This is more of a question than an issue :-)
I have migrated my application from v1.7.0 to v2.0.1 in the development branch, except for the sockets functionality (currently, I use only the Spot lib). The critical part here is to monitor updates (closed orders) through sockets.
I tried to find how to use sockets within v2.0.1, but it seems this functionality has not been added yet.
So, my question: did I miss something? If socket functionality has not been added yet, when can we expect it? Do you have any estimation or plans you can share?
Hello @Jay04653 ,
Indeed for now websocket API and websocket Stream are not supported in the new PHP SDK. We are currently evaluating the efforts to support those.
Thank you for your feedback.
+1 for WebSocket support in v2. This would be really helpful since the v1.7 (410 errors on newListenKey) no longer works.
best regards
+1 for WebSocket support in v2. This would be really helpful since the v1.7 (410 errors on newListenKey) no longer works.
best regards
Here is the custom code I use to work with sockets. I hope it helps.
<?php
namespace app\models\proxy;
use React\EventLoop\Loop;
use Ratchet\Client\Connector;
use Ratchet\Client\WebSocket;
use Ratchet\RFC6455\Messaging\Message;
use app\models\db\MarketsAccounts;
class BinanceSpotSocket
{
const DEFAULT_ACCOUNT = 1;
private $_baseUrl = "wss://ws-api.binance.com:443/ws-api/v3";
private $_account;
public function __construct($account = self::DEFAULT_ACCOUNT)
{
if (!$account instanceof MarketsAccounts)
$account = MarketsAccounts::findOne($account);
if (!$account) throw new \Exception('Account not found.');
$this->_account = $account;
}
public function userDataStream(callable $callback)
{
$apiKey = $this->_account->api_key;
$secretKey = $this->_account->secret_key;
$method = 'userDataStream.subscribe.signature';
$loop = Loop::get();
$connector = new Connector($loop);
$connector($this->_baseUrl)->then(
function (WebSocket $conn) use ($apiKey, $secretKey, $method, $callback, $loop) {
$request = $this->_buildSubscriptionRequest($apiKey, $secretKey, $method);
$conn->send($request);
$subscriptionRequestData = json_decode($request, true);
$subscriptionResponseId = $subscriptionRequestData['id'];
$conn->on('message', function (Message $msg) use ($conn, $callback, $subscriptionResponseId) {
$message = (string) $msg;
$data = json_decode($message);
if (isset($data->id) && $data->id === $subscriptionResponseId) {
if (isset($data->status) && $data->status !== 200) {
$conn->close(1000, "Subscription Failed: $message");
return;
}
return;
}
if (isset($data->event) && isset($data->event->e) && $data->event->e === 'eventStreamTerminated') {
$conn->close(1000, 'Stream Terminated by Server');
return;
}
if (isset($data->method) && $data->method === 'ping') {
$conn->send(json_encode(['id' => $data->id, 'method' => 'pong']));
return;
}
call_user_func($callback, $this, $data);
});
$conn->on('close', function ($code = null, $reason = null) use ($loop) {
$loop->stop();
});
},
function (\Exception $e) use ($loop) {
$loop->stop();
throw $e;
}
);
$loop->run();
}
private function _buildSubscriptionRequest($apiKey, $secretKey, $method)
{
$params = [
'apiKey' => $apiKey,
'timestamp' => (int) round(microtime(true) * 1000)
];
ksort($params);
$queryString = http_build_query($params, '', '&', PHP_QUERY_RFC3986);
$signature = hash_hmac('sha256', $queryString, $secretKey);
$params['signature'] = $signature;
return json_encode([
'id' => uniqid(),
'method' => $method,
'params' => $params
]);
}
}
?>
@Jay04653 I did it in same way for now, but thanks :)