/nsq-swoole

PHP Swoole client for NSQ

Primary LanguagePHP

NSQ-SWOOLE

PHP Swoole client for NSQ.

Requirements

  • PHP 5.4 or higher
  • Swoole 1.8.6 or higher

Installation

composer require asan/nsq-swoole

Testing it out

Publish:

php tests/PublishTest.php

Subscribe:

php tests/SubscribeTest.php

Publishing

The client supports publishing to N nsqd servers. which must be specified explicitly by hostname. And supports publishing multiple messages.

$client = new Asan\Nsq\Client;

$client->publishTo([
    ['host' => 'localhost', 'port' => 4150]
])->publish('test', 'single message');

//multiple messages
$client->publish('test', ['message one', 'message two']);

//HA publishing:
$client->publishTo([
    ['host' => 'nsq1', 'port' => 4150],
    ['host' => 'nsq2', /*'port' => 4150*/]
], Asan\Nsq\Client::PUB_QUORUM)->publish('test', 'HA publishing message');

Subscribing

The client supports subscribing from N nsqd servers, each of which will be auto-discovered from one or more nslookupd servers. The way this works is that nslookupd is able to provide a list of auto-discovered nodes hosting messages for a given topic.

$lookup = new Asan\Nsq\Lookup\Lookupd([
    ['host' => 'nsq1', 'port' => 4161],
    ['host' => 'nsq2', /*'port' => 4161*/]
]);

$client = new Asan\Nsq\Client;

$client->subscribe($lookup, 'test', 'web', function($moniter, $msg) {
    echo sprintf("READ\t%s\t%s\n", $msg->getId(), $msg->getPayload());
});