Permalink
Browse files

Create pubsub-with-channels.php

Almost the same as pubsub with multi part messages allowing to send subscription channel and message data separately
  • Loading branch information...
1 parent 06fb7f9 commit 5f86d5134ab18a8fbdd8c5de1902f8eb89dd7690 @tkleinhakisa tkleinhakisa committed Feb 11, 2015
Showing with 33 additions and 0 deletions.
  1. +33 −0 examples/pubsub-with-channels.php
@@ -0,0 +1,33 @@
+<?php
+
+require __DIR__.'/../vendor/autoload.php';
+
+$loop = React\EventLoop\Factory::create();
+
+$context = new React\ZMQ\Context($loop);
+
+$sub = $context->getSocket(\ZMQ::SOCKET_SUB);
+$sub->connect('tcp://127.0.0.1:5555');
+$sub->subscribe('sub');
+$sub->on('messages', function ($msg) {
+ echo "Received: ". $msg[1] ." on channel: ". $msg[0] ."\n";
+});
+
+$bus = $context->getSocket(\ZMQ::SOCKET_SUB);
+$bus->connect('tcp://127.0.0.1:5555');
+$bus->subscribe('bus');
+$bus->on('messages', function ($msg) {
+ echo $msg[0] ." :lennahc no ". $msg[1] ." :devieceR\n";
+});
+
+$pub = $context->getSocket(\ZMQ::SOCKET_PUB);
+$pub->bind('tcp://127.0.0.1:5555');
+$i = 0;
+$loop->addPeriodicTimer(1, function () use (&$i, $pub) {
+ $i++;
+ echo "publishing $i\n";
+ $pub->sendmulti(array('sub', $i)); // you get this one in the sub socket
+ $pub->sendmulti(array('bus', $i)); // you don't get this one
+});
+
+$loop->run();

0 comments on commit 5f86d51

Please sign in to comment.