1. Go to this page and download the library: Download easyswoole/nsq 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/ */
easyswoole / nsq example snippets
use EasySwoole\Nsq\Config;
use EasySwoole\Nsq\Nsq;
= new Nsq(new Config(host: '127.0.0.1', port: 4150));
try {
$nsq->publish('topic.tests', 'hello');
$nsq->multiPublish('topic.tests', ['first', 'second']);
$nsq->deferredPublish('topic.tests', 'later', 1000); // 毫秒
} finally {
$nsq->close();
}
});
$lookup = new EasySwoole\Nsq\Lookup\Nsqlookupd(
['https://nsqlookupd.example.com:4162'],
tls: new EasySwoole\Nsq\TlsConfig(caFile: '/etc/nsq/tls/ca.pem'),
);
use EasySwoole\Nsq\Config;
use EasySwoole\Nsq\Lookup\Nsqlookupd;
use EasySwoole\Nsq\Nsq;
:4161']);
foreach ($lookup->lookupHosts('topic.tests') as $endpoint) {
Swoole\Coroutine::create(function () use ($endpoint): void {
$address = parse_url('tcp://' . $endpoint);
$consumer = new Nsq(new Config(
host: trim($address['host'], '[]'),
port: $address['port'],
readTimeout: -1,
));
try {
$consumer->subscribe('topic.tests', 'tests.consuming');
$consumer->consume(function ($message): void { echo $message->body; });
} finally {
$consumer->close();
}
});
}
});
sh
php tests/mock-nsqd.php 4150
# 可选认证:MOCK_NSQ_AUTH_SECRET=secret php tests/mock-nsqd.php 4150
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.