Websocket API closes 1 second after subscribe

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.

  1. how long before the connection dead?
  2. have you received ping message from server, it’s required to respond with pong.
  3. If you don’t exit the process when seeing err return, can you continue receiving message?
  1. I added some extra debugging. Total connect time of each attempt is around 1.2 seconds, with the time between successful subscribe (me) and the close opcode (binance) being around 0.2 seconds.

  2. There is no ping ever received, the connection effectively closes immediately.

  3. If I modify the client code to ignore opcode 8 (close), the remote end closes the TCP socket about 10 seconds later.

odd, but hard to determine the reasons for now. There is no geneal issue from server side. Most of our API traders are connecting with the websocket server.

What I can suggest is:

  • connect with websocat, which is a cli websocket client tool. This can be helpful to find out if there is any issue from network.
    • if you can’t with websocat, change the network or from a different client.
    • if you can connect with it, then please focus on debug on the app. Check if there is any issue with the php websocket library, etc.