Skip to content

mssayari/binance-ws

 
 

Repository files navigation

Binance-WS for Laravel

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.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%