PHP code example of prooph / psb-zeromq-producer

1. Go to this page and download the library: Download prooph/psb-zeromq-producer library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

prooph / psb-zeromq-producer example snippets


// file: CLIENT.php

$container = new Container;
$container['config'] = [
    'prooph' => [
        'zeromq_producer' => [
            'dsn' => 'tcp://127.0.0.1:5556', // ZMQ Server Address.
            'persistent_id' => 'example', // ZMQ Persistent ID to keep connections alive between requests.
            'rpc' => true, // Use as Query Bus.
        ]
    ]
];

$factory = Prooph\ServiceBus\Message\ZeroMQ\Container\ZeroMQMessageProducerFactory;
$zmqProducer = $factory($container);

// Setup complete, now to add it to the prooph service bus.

$queryBus = new Prooph\ServiceBus\QueryBus();
$router = new Prooph\ServiceBus\Plugin\Router\QueryRouter();
$router->route('ExampleQuery')
    ->to($zmqProducer);

$queryBus->utilize($router);
$getText = new ExampleQuery('Hello Server.');
$promise = $queryBus->dispatch($getText);

$promise->then(function ($response) {
    var_dump($response); // string "Hello Client."
});

exit(0);

// file: SERVER.php


$context = new ZMQContext;
$socket = new ZMQSocket($context, ZMQ::SOCKET_REP);
$socket->bind('tcp://127.0.0.1:5556');

echo "ZMQ Stub Server Started.";

while ($message = $socket->recv()) {
    if ('Hello Server.' === $message) {
        $socket->send('Hello Client.');
    }
}