
The day will come, and you will understand that a single thread in PHP is not enough for you.
First you optimize the code, then you try to change the mind to an asynchronous reactor, but the whole PHP world does not want to understand such a desire. You look at phthreads, but after java concurrency you feel that you have been “deceived” somewhere. And when you conceive to leave the process and start to choke in forks exec'ah and signals, you will understand that it is impossible to dive further. And finally, emerging from all this, you will sail to the island of MOM (message-oriented middleware).
Ohhh, but what kind of sellers are there only Kirbies: RabbitMQ, ActiveMQ, Kafka, Kestrel and even Redis pub / sub podbryzhivaet. And everyone is doing well: all the best, fast, trouble-free. But there is a small misfortune - a step to the side and, hello, now you are among the crowd of whiners on stackoverflow in search of work rounds and strange schemes. And this will continue until you find ZeroMQ.
')
What is it? ZeroMQ is a high-performance asynchronous message-oriented library designed for use in scalable distributed or parallel applications. But unlike other message-oriented middleware, it is broker-free and works without a server as such.

And what is being offered then? And these guys offer a set of sockets on steroids optimized for basic message patterns and we can use them as we want. With their help, we can build a network with any topology and complexity.
They also have their own sectarian dock
http://zguide.zeromq.org/page:all . Well sets the brain in the right direction, regardless of whether you use 0mq or not, however, if you can in multi-threaded programming, you can partially scroll through.
The bottom line:
- Steroid Socket Set
- Damn fast
- Work via IPC, TCP, multicast, inproc
- Asynchronous
- Easy start
- Over 40 40 programming languages
It sounds all very cool! Enough of the theories go to
www.gliffy.com and violently architect the system. And we want the following:

- Auth + task generator
Responsible for authorization on the server and distributes non-stop tasks for parsing. - Parse worker
Gets the authorization key and the task to parse after completion returns the result to the generator. - Parsed data publisher
Having received the result from the worker, it will be published to all subscriber. - Alert system subscriber
Receives data and sends alerts if needed. - Upload system subscriber
Receives data and uploads it to the server. - Data upload worker
Enkodit, zipuet and applaud the data on the server. - System monitor
Collects system statistics in real time.
The architecture turned out to be simple, all components were written separately from the business logic, decorated in a package for a composer and filled in on the githab:
https://github.com/limitium/zmqTo pick it up, you need (for example, Debian):
1. Install ZeroMQ
sudo apt-get update -qq sudo apt-get install -y libzmq3-dev
2. Install binding php-zmq
git clone https://github.com/mkoppanen/php-zmq.git sh -c "cd php-zmq && phpize && ./configure && make --silent && sudo make install" echo "extension=zmq.so" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`
3. Install via composer
composer require limitium/zmq
Next, take, for example, psr-3 logger and see how it works:
Logger
$logger = new ZLogger('my_service_1', 'tcp://127.0.0.1:5555'); $logger->info("core is stable"); $logger->emergency("we're all going to die!");
Log collector
(new Concentrator('tcp://127.0.0.1:5555')) ->setReceiver(function ($logMsg) { $serviceName = $logMsg[0]; $time = $logMsg[1]; $logLevel = $logMsg[2]; $logMsg = $logMsg[3]; }) ->listen();
Everything is simple, while the logger, thanks to the ZeroMQ buns, can work both within one process and collect information from 100,500 servers.
Example of task generator and worker
Generator
(new Ventilator('tcp://127.0.0.1:5555')) ->setGenerator(function () { sleep(1); return rand(); }) ->setResponder(function ($msg) { echo $msg; }) ->listen();
Worker
(new Worker('tcp://127.0.0.1:5555')) ->setExecutor(function ($msg) { return $msg + $msg; }) ->work();
And at the end of the banal pub / sub
Publisher
$pub = new Publisher('tcp://127.0.0.1:5555'); $pub->send('azaza');
Subscriber
(new Subscriber('tcp://127.0.0.1:5555')) ->setListener(function ($msg){ echo $msg; }) ->listen();
The only minus of ZeroMQ, which is worth noting - the more interpticity you want from the system, the more code you have to write. But who cares when everything runs in 2 lines of code?