6e37bbd1ae0aa8f6baa8c682bf10bd17d0808131
Nsq PHP

PHP Client for NSQ.
This library follow SemVer. Until version 1.0 will be released anything MAY change at any time, public API SHOULD NOT be considered stable. If you want use it before stable version was released install strict version without range.
Installation
This library is installable via Composer:
composer require nsq/nsq
Requirements
This library requires PHP 8.0 or later.
Although not required, it is recommended that you install the phpinnacle/ext-buffer to speed up phpinnacle/buffer .
Features
- PUB
- SUB
- Feature Negotiation
- Discovery
- Backoff
- TLS
- Snappy
- Sampling
- AUTH
Usage
Publish
use Nsq\Producer;
$producer = new Producer(address: 'tcp://nsqd:4150');
// Publish a message to a topic
$producer->pub('topic', 'Simple message');
// Publish multiple messages to a topic (atomically)
$producer->mpub('topic', [
'Message one',
'Message two',
]);
// Publish a deferred message to a topic
$producer->dpub('topic', 5000, 'Deferred message');
Subscription
use Nsq\Consumer;
use Nsq\Message;
use Nsq\Subscriber;
$consumer = new Consumer('tcp://nsqd:4150');
$subscriber = new Subscriber($consumer);
$generator = $subscriber->subscribe('topic', 'channel', timeout: 5);
foreach ($generator as $message) {
if ($message instanceof Message) {
$payload = $message->body;
// handle message
$message->touch(); // Reset the timeout for an in-flight message
$message->requeue(timeout: 5000); // Re-queue a message (indicate failure to process)
$message->finish(); // Finish a message (indicate successful processing)
}
// In case of nothing received during timeout generator will return NULL
// Here we can do something between messages, like pcntl_signal_dispatch()
// We can also communicate with generator through send
// for example:
// Dynamically change timeout
$generator->send(Subscriber::CHANGE_TIMEOUT);
$generator->send(10.0); // float required
// Gracefully close connection (loop will be ended)
$generator->send(Subscriber::STOP);
}
Integrations
License:
The MIT License (MIT). Please see LICENSE
for more information.
Description
Languages
PHP
95.9%
Dockerfile
3.7%
Makefile
0.4%