107 lines
3.1 KiB
Markdown
107 lines
3.1 KiB
Markdown
# Nsq PHP
|
|
|
|
<img src="https://github.com/nsqphp/nsqphp/raw/main/docs/logo.png" alt="" align="left" width="150">
|
|
|
|
PHP Client for [NSQ](https://nsq.io/).
|
|
|
|
[](//packagist.org/packages/nsq/nsq) [](//packagist.org/packages/nsq/nsq) [](//packagist.org/packages/nsq/nsq)
|
|
[](https://codecov.io/gh/nsqphp/nsqphp) [](https://dashboard.stryker-mutator.io/reports/github.com/nsqphp/nsqphp/main)
|
|
|
|
This library follow [SemVer](https://semver.org/). 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](https://getcomposer.org/):
|
|
|
|
```bash
|
|
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](https://github.com/phpinnacle/ext-buffer) to speed up [phpinnacle/buffer](https://github.com/phpinnacle/buffer) .
|
|
|
|
Features
|
|
--------
|
|
|
|
- [x] PUB
|
|
- [x] SUB
|
|
- [X] Feature Negotiation
|
|
- [ ] Discovery
|
|
- [ ] Backoff
|
|
- [ ] TLS
|
|
- [ ] Deflate
|
|
- [X] Snappy
|
|
- [X] Sampling
|
|
- [X] AUTH
|
|
|
|
Usage
|
|
-----
|
|
|
|
### Producer
|
|
|
|
```php
|
|
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', 'Deferred message', delay: 5000);
|
|
```
|
|
|
|
### Consumer
|
|
|
|
```php
|
|
use Nsq\Consumer;
|
|
use Nsq\Protocol\Message;
|
|
|
|
$consumer = new Consumer(
|
|
topic: 'topic',
|
|
channel: 'channel',
|
|
address: 'tcp://nsqd:4150',
|
|
);
|
|
|
|
// Simple blocking loop based on generator
|
|
$generator = $consumer->generator();
|
|
|
|
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()
|
|
|
|
// Gracefully close connection (loop will be ended)
|
|
$generator->send(0);
|
|
}
|
|
```
|
|
|
|
### Integrations
|
|
|
|
- [Symfony](https://github.com/nsqphp/NsqBundle)
|
|
|
|
License:
|
|
--------
|
|
|
|
The MIT License (MIT). Please see [`LICENSE`](./LICENSE) for more information.
|