When using a simple Websocket PHP library, the connect, upgrade and subscribe succeeds. Sometimes a small amount of the expected data comes through, BUT, within a second or two the remote end requests a close.
There is no useful error returned other than that the remote end sent opcode 8 (close).
I tried ignoring the higher level close request but Binance closes the TCP socket anyway.
I am wondering if anyone else has experienced a similar problem. This code works fine at another exchange - the stream stays connected and returns data for hours - so I don’t know where the issue is.
Here is an example showing multiple subscribe attempts:
*** Connecting to server: stream.binance.com at 2022-03-06 16:01:23 UTC+0
websocket: connecting to ssl://stream.binance.com:9443 with path /ws/btcusdt@trade
websocket: connected
websocket: sent upgrade request
websocket: successful upgrade
Sending message to server: '{"method":"SUBSCRIBE","params":["btcusdt@trade"],"id":1}'
{"e":"trade","E":1646582484683,"s":"BTCUSDT","t":1282369799,"p":"38826.53000000","q":"0.00336000","b":9689374275,"a":9689374631,"T":1646582484683,"m":true,"M":true}
{"e":"trade","E":1646582484698,"s":"BTCUSDT","t":1282369800,"p":"38826.54000000","q":"0.02328000","b":9689374632,"a":9689374152,"T":1646582484697,"m":false,"M":true}
{"e":"trade","E":1646582484776,"s":"BTCUSDT","t":1282369801,"p":"38826.54000000","q":"0.00057000","b":9689374645,"a":9689374152,"T":1646582484775,"m":false,"M":true}
{"e":"trade","E":1646582484792,"s":"BTCUSDT","t":1282369802,"p":"38826.53000000","q":"0.02472000","b":9689374275,"a":9689374651,"T":1646582484791,"m":true,"M":true}
errstr=Remote sent opcode 8 (close)
*** Connecting to server: stream.binance.com at 2022-03-06 16:01:24 UTC+0
websocket: connecting to ssl://stream.binance.com:9443 with path /ws/btcusdt@trade
websocket: connected
websocket: sent upgrade request
websocket: successful upgrade
Sending message to server: '{"method":"SUBSCRIBE","params":["btcusdt@trade"],"id":1}'
{"e":"trade","E":1646582485891,"s":"BTCUSDT","t":1282369805,"p":"38826.54000000","q":"0.00257000","b":9689374751,"a":9689374152,"T":1646582485891,"m":false,"M":true}
errstr=Remote sent opcode 8 (close)
*** Connecting to server: stream.binance.com at 2022-03-06 16:01:26 UTC+0
websocket: connecting to ssl://stream.binance.com:9443 with path /ws/btcusdt@trade
websocket: connected
websocket: sent upgrade request
websocket: successful upgrade
Sending message to server: '{"method":"SUBSCRIBE","params":["btcusdt@trade"],"id":1}'
errstr=Remote sent opcode 8 (close)
Can someone else using PHP give this simple code a try for me?
Requires https://raw.githubusercontent.com/paragi/PHP-websocket-client/master/websocket_client.php
require_once("PHP-websocket-client/websocket_client.php");
// git clone https://github.com/paragi/PHP-websocket-client.git
$server = "stream.binance.com";
$serverport = 9443;
$streamname = "btcusdt@trade";
$t = array("method" => "SUBSCRIBE", "params" => array("$streamname"), "id" => 1);
$uri = "/ws/$streamname";
$message = json_encode($t) . "\n";
echo "\n*** Connecting to server: $server at " . gmdate("Y-m-d H:i:s") . " UTC+0\n";
$sp = websocket_open($server, $serverport,'',$errstr, 30, true, false, $uri);
if ($sp) {
echo "Sending message to server: '" . trim($message) . "' \n";
websocket_write($sp,$message);
while (1) {
$r = websocket_read($sp,$errstr);
echo "$r\n";
if ($r == "") {
echo "errstr=$errstr\n";
die;
}
}
}
?>
Thank you.