notf0und/binance-ws

Binance Websockets Client for Laravel

0.3.1 2021-05-29 05:18 UTC

This package is auto-updated.

Last update: 2024-04-27 09:56:03 UTC


README

A websockets client to get data from cryptocurrency exchange Binance.

Installation

composer require notf0und/binance-ws

Available streams:

  • AggregateTrade
  • AllBookTickers
  • IndividualSymbolBookTicker
  • IndividualSymbolMiniTicker
  • IndividualSymbolTicker
  • KlineCandlestick
  • Trade

Usage example

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);
    }
}

Change log

Please see the changelog for more information on what has changed recently.

Contributing

Please see contributing.md for details and a todolist.

Security

If you discover any security related issues, please email gonzartur@gmail.com instead of using the issue tracker.

Credits

License

MIT. Please see the license file for more information.