PHP code example of asan / nsq-swoole

1. Go to this page and download the library: Download asan/nsq-swoole library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

asan / nsq-swoole example snippets


$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');

$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());
});