Enqueue Symfony’s process components with PHP and ZeroMQ

Today I’d like to play with ZeroMQ. ZeroMQ is a great tool to work with sockets. I will show you the problem that I want to solve: One web application needs to execute background processes but I need to execute those processes in order. Two users cannot execute one process at the same time. OK, if we face to this problem we can use Gearman. I’ve written various posts about Gearman (here and here for example). But today I want to play with ZeroMQ.

I’m going to use one great library called React. With react (reactor pattern implementation in PHP) we can do various thing. One of them are ZeroMQ bindings.

In this simple example we are going to build a simple server and client. The client will send to the server one string that the server will enqueue and executes using the Symfony’s Process component.

Here is the client:

include __DIR__ . '/../vendor/autoload.php';

use Zmqlifo\Client;

$queue = Client::factory('tcp://');
echo $queue->run("ls -latr")->getOutput();
echo $queue->run("pwd")->getOutput();

And finally the server:

include __DIR__ . '/../vendor/autoload.php';

use Symfony\Component\Process\Process;
use Zmqlifo\Server;

$server = Server::factory('tcp://');
$server->registerOnMessageCallback(function ($msg) {
    $process = new Process($msg);
    return $process->getOutput();


You can see the working example here:

you can check the full code of the library in github and Packagist.

As Igor Wiedler said React is not necessary here.

ZMQ is used for blocking sends
and blocking tasks, having an event loop does not really make much sense.

github repository updated (thanks!).


3 thoughts on “Enqueue Symfony’s process components with PHP and ZeroMQ

  1. I might have missed something, but won’t `$queue->run(“ls -latr”)->getOutput();` block due to ZMQSocket::recv waiting for a response? You can set ZMQ::MODE_NOBLOCK but you’ll need to loop over recv() until the response is ready. Is that right?

    1. In this example blocking was exactly what I wanted, but I want to check with ZMQ::MODE_NOBLOCK. In this case I think I must use React again for the event loop.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.