A websockets client to get data from cryptocurrency exchange Binance.
composer require notf0und/binance-ws
- AggregateTrade
- AllBookTickers
- IndividualSymbolBookTicker
- IndividualSymbolMiniTicker
- IndividualSymbolTicker
- KlineCandlestick
- Trade
use Notf0und\BinanceWS\Services\Binance\Websockets\AggregateTrade;
use Notf0und\BinanceWS\Services\Binance\Websockets\AllBookTickers;
use Notf0und\BinanceWS\Services\Binance\Websockets\KlineCandlestick;
use Notf0und\BinanceWS\Services\Binance\Websockets\Client\Ratchet as Client;
// Default config (btcusdt symbol)
$allBookTickers = new AllBookTickers();
// Change symbol
$aggregateTrade = new AggregateTrade();
$aggregateTrade->setSymbol('ethbtc');
// Change symbol and interval (just KlineCandlestick class allow to set interval)
$kline = new KlineCandlestick();
$kline->setSymbol('ethusdt');
$kline->setInterval('15m');
// Create the client with the previously created streams
$client = new Client([
$allBookTickers,
$aggregateTrade,
$kline
]);
//Connect
$client->connect()
If everything goes well, each message received would be firing the event \Notf0und\BinanceWS\Events\MessageReceived containing the payload attribute.
So now we can attach to it some event listener/s, like the following.
// App\Providers\EventServiceProvider.php
use Notf0und\BinanceWS\Events\MessageReceived;
use App\Listeners\MyCustomEventListener;
class EventServiceProvider extends ServiceProvider
{
/**
* The event listener mappings for the application.
*
* @var array
*/
protected $listen = [
MessageReceived::class => [
MyCustomEventListener::class
]
];
On the event listener we can, for example, write the content received to the log:
namespace App\Listeners;
class MyCustomEventListener
{
/**
* Handle the event.
*
* @param object $event
* @return void
*/
public function handle($event)
{
Log::info($event->payload);
}
}
Please see the changelog for more information on what has changed recently.
Please see contributing.md for details and a todolist.
If you discover any security related issues, please email gonzartur@gmail.com instead of using the issue tracker.
MIT. Please see the license file for more information.