Skip to content

Commit

Permalink
Add WebSocket kline() function for realtime data
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Eyrick authored Apr 10, 2018
1 parent 224e8af commit 7b2a029
Showing 1 changed file with 54 additions and 7 deletions.
61 changes: 54 additions & 7 deletions php-binance-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,7 @@ public function order( string $side, string $symbol, $quantity, $price, string $
}

/**
* candlesticks get the cancles for the given intervals
* candlesticks get the candles for the given intervals
* 1m,3m,5m,15m,30m,1h,2h,4h,6h,8h,12h,1d,3d,1w,1M
*
* $candles = $api->candlesticks("BNBBTC", "5m");
Expand Down Expand Up @@ -1672,11 +1672,10 @@ public function chart( $symbols, string $interval = "30m", Callable $callback )
$this->chartQueue[ $symbol ] = [];
$this->chartQueue[ $symbol ][ $interval ] = [];
$this->info[ $symbol ][ $interval ][ 'firstOpen' ] = 0;
// $this->info[$symbol]['chartCallback'.$interval] = $callback;
$endpoint = strtolower( $symbol ) . '@kline_' . $interval;
$this->subscriptions[ $endpoint ] = true;
$connector( $this->stream . $endpoint )->then( function ( $ws ) use ($callback, $symbol, $loop, $endpoint, $interval ) {
$ws->on( 'message', function ( $data ) use ($ws, $loop, $callback, $endpoint ) {
$connector( $this->stream . $endpoint )->then( function ( $ws ) use ( $callback, $symbol, $loop, $endpoint, $interval ) {
$ws->on( 'message', function ( $data ) use ( $ws, $loop, $callback, $endpoint ) {
if( $this->subscriptions[ $endpoint ] === false ) {
//$this->subscriptions[$endpoint] = null;
$loop->stop();
Expand All @@ -1689,11 +1688,11 @@ public function chart( $symbols, string $interval = "30m", Callable $callback )
$this->chartHandler( $symbol, $interval, $json );
call_user_func( $callback, $this, $symbol, $this->charts[ $symbol ][ $interval ] );
} );
$ws->on( 'close', function ( $code = null, $reason = null ) use ($symbol, $loop, $interval ) {
$ws->on( 'close', function ( $code = null, $reason = null ) use ( $symbol, $loop, $interval ) {
echo "chart({$symbol},{$interval}) WebSocket Connection closed! ({$code} - {$reason})" . PHP_EOL;
$loop->stop();
} );
}, function ( $e ) use ($loop, $symbol, $interval ) {
}, function ( $e ) use ( $loop, $symbol, $interval ) {
echo "chart({$symbol},{$interval})) Could not connect: {$e->getMessage()}" . PHP_EOL;
$loop->stop();
} );
Expand All @@ -1702,11 +1701,59 @@ public function chart( $symbols, string $interval = "30m", Callable $callback )
$this->chartHandler( $symbol, $interval, $json );
}
$this->chartQueue[ $symbol ][ $interval ] = [];
// $this->info[$symbol]['chartCallback'.$interval]($this, $symbol, $this->charts[$symbol][$interval]);
call_user_func( $callback, $this, $symbol, $this->charts[ $symbol ][ $interval ] );
}
$loop->run();
}

/**
* kline Subscribes to @klines WebSocket endpoint for latest chart data only
*
* $api->kline(["BNBBTC"], "15m", function($api, $symbol, $chart) {
* echo "{$symbol} chart update\n";
* print_r($chart);
* });
*
* @param $symbols string required symbols
* @param $interval string time inteval
* @param $callback callable closure
* @return null
* @throws \Exception
*/
public function kline( $symbols, string $interval = "30m", Callable $callback ) {
if( !is_array( $symbols ) )
$symbols = [
$symbols
];
$loop = \React\EventLoop\Factory::create();
$react = new \React\Socket\Connector( $loop );
$connector = new \Ratchet\Client\Connector( $loop, $react );
foreach( $symbols as $symbol ) {
$endpoint = strtolower( $symbol ) . '@kline_' . $interval;
$this->subscriptions[ $endpoint ] = true;
$connector( $this->stream . $endpoint )->then( function ( $ws ) use ( $callback, $symbol, $loop, $endpoint, $interval ) {
$ws->on( 'message', function ( $data ) use ( $ws, $loop, $callback, $endpoint ) {
if( $this->subscriptions[ $endpoint ] === false ) {
$loop->stop();
return;
}
$json = json_decode( $data );
$chart = $json->k;
$symbol = $json->s;
$interval = $chart->i;
call_user_func( $callback, $this, $symbol, $this->chart );
} );
$ws->on( 'close', function ( $code = null, $reason = null ) use ( $symbol, $loop, $interval ) {
echo "kline({$symbol},{$interval}) WebSocket Connection closed! ({$code} - {$reason})" . PHP_EOL;
$loop->stop();
} );
}, function ( $e ) use ( $loop, $symbol, $interval ) {
echo "kline({$symbol},{$interval})) Could not connect: {$e->getMessage()}" . PHP_EOL;
$loop->stop();
} );
}
$loop->run();
}

/**
* terminate Terminates websocket endpoints. View endpoints first: print_r($api->subscriptions)
Expand Down

0 comments on commit 7b2a029

Please sign in to comment.