denpamusic / laravel-zeromq

Fully unit-tested ZeroMQ driver for Laravel.
MIT License
17 stars 7 forks source link
laravel laravel-package messaging php pubsub pushpull websockets zeromq zmq

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"
  2. (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

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.