Example #1
0
<?php

/**
 * Test subscription
 * 
 * Subscribes to "mytopic" topic using channel "foo" (or one provided as argv1).
 * Talks to nsqlookupd on localhost (by default), or one(s) provided as argv2.
 * 
 * php test-io.php bar nsq1,nsq2
 */
include __DIR__ . '/../bootstrap.php';
$hosts = isset($argv[2]) ? $argv[2] : 'localhost';
$logger = new nsqphp\Logger\Stderr();
$dedupe = new nsqphp\Dedupe\OppositeOfBloomFilterMemcached();
$lookup = new nsqphp\Lookup\Nsqlookupd($hosts);
$requeueStrategy = new nsqphp\RequeueStrategy\FixedDelay();
$nsq = new nsqphp\nsqphp($lookup, $dedupe, $requeueStrategy, $logger);
$channel = isset($argv[1]) ? $argv[1] : 'foo';
$nsq->subscribe('mytopic', $channel, function ($msg) {
    echo "READ\t" . $msg->getId() . "\t" . $msg->getPayload() . "\n";
});
$nsq->run();
Example #2
0
<?php

/**
 * Test pub
 * 
 * Pubs N message to "mytopic" topic, where N defaults to 10 but can be
 * supplied as argv1. Connects to nsqd on localhost, or argv2 (which can
 * be a , separated list of hosts). We can also specify how many replicas
 * must respond to consider operation a success.
 * 
 * php test-pub.php 100 nsq1,nsq2 1
 */
include __DIR__ . '/../bootstrap.php';
$n = isset($argv[1]) ? (int) $argv[1] : 10;
$hosts = isset($argv[2]) ? explode(',', $argv[2]) : array('localhost');
$replicas = isset($argv[3]) ? $argv[3] : 1;
$runId = md5(microtime(TRUE));
$nsq = new nsqphp\nsqphp();
$nsq->publishTo($hosts, $replicas);
for ($i = 1; $i <= $n; $i++) {
    $nsq->publish('mytopic', new nsqphp\Message\Message(json_encode(array('msg' => $i, 'run' => $runId))));
    echo "Published {$i} to `mytopic`\n";
}