/laravel-zeromq

Fully unit-tested ZeroMQ driver for Laravel.

Primary LanguagePHPMIT LicenseMIT

ZeroMQ driver for Laravel

Latest Stable Version License Build Status Code Climate Code Coverage

About

Fully unit-tested Laravel ZeroMQ driver based on react/zmq.

Installation

  1. Install package:
composer require denpa/laravel-zeromq "^1.0"
  1. (skip if using Laravel 5.5 or newer) Add service provider and facade to ./config/app.php
'providers' => [
    ...
    Denpa\ZeroMQ\Providers\ServiceProvider::class,
];
'aliases' => [
    ...
    'ZeroMQ' => Denpa\ZeroMQ\Facades\ZeroMQ::class,
];
  1. Publish config file php artisan vendor:publish --provider="Denpa\ZeroMQ\Providers\ServiceProvider" in your project directory.

Requirements

  • PHP 7.1 or higher
  • ZMQ PHP extension
  • Laravel 5.3 or higher

Usage

Publish:

zeromq()->publish(['foo', 'bar'], 'hello');
zeromq()->connection('test')->publish(['foo', 'bar'], 'hello');

Pull:

zeromq()->pull(function ($message) {
    echo $message;
});

Push:

zeromq()->push('hello');

Subscribe:

zeromq()->subscribe(['foo', 'bar'], function ($message) {
    echo $message;
});

Facade

use Denpa\ZeroMQ\Facades\ZeroMQ;

$callback = function ($message) {
    echo $message;
};

// use default connection
ZeroMQ::publish(['foo', 'bar'], 'hello');
ZeroMQ::pull($callback);
ZeroMQ::push('hello');
ZeroMQ::subscribe(['foo', 'bar'], $callback);

// use different connection
ZeroMQ::connection('baz')->push('hello');

Broadcasting

Set BROADCAST_DRIVER=zeromq in environment file and add following lines

'zeromq' => [
    'driver' => 'zeromq',
],

to 'connections' key in config/broadcasting.php.

Now use laravel broadcast($event); helper to broadcast events via ZeroMQ.

Bitcoin Core (laravel-bitcoinrpc)

laravel-bitcoinrpc integrates this package to subscribe to topics broadcasted by Bitcoin Core (and some forks).

bitcoind()->on('hashblock', function ($blockhash, $sequence) {
    // get hash of new best block and retrieve full block info
    $block = bitcoind()->getBlock($blockhash);
    print_r($block->get());
});

For more info, visit laravel-bitcoinrpc documentation.

License

This product is distributed under MIT license.