Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/byrnedo/php-nats-streaming
Client for nats streaming server in php
https://github.com/byrnedo/php-nats-streaming
message-queue messaging nats-streaming-server php php71
Last synced: about 1 month ago
JSON representation
Client for nats streaming server in php
- Host: GitHub
- URL: https://github.com/byrnedo/php-nats-streaming
- Owner: byrnedo
- License: mit
- Created: 2017-11-09T12:14:38.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2017-12-04T15:38:25.000Z (almost 7 years ago)
- Last Synced: 2024-09-29T19:41:35.107Z (about 2 months ago)
- Topics: message-queue, messaging, nats-streaming-server, php, php71
- Language: PHP
- Size: 121 KB
- Stars: 44
- Watchers: 6
- Forks: 21
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# PHP Nats Streaming Server Client
## Build
| Master | Develop |
| ------------- | ------------- |
| [![Build Status](https://travis-ci.org/byrnedo/php-nats-streaming.svg?branch=master)](https://travis-ci.org/byrnedo/php-nats-streaming) | [![Build Status](https://travis-ci.org/byrnedo/php-nats-streaming.svg?branch=develop)](https://travis-ci.org/byrnedo/php-nats-streaming) |## Coverage
| Master | Develop |
| ------------- | ------------- |
| [![Coverage Status](https://coveralls.io/repos/github/byrnedo/php-nats-streaming/badge.svg?branch=master)](https://coveralls.io/github/byrnedo/php-nats-streaming?branch=master) | [![Coverage Status](https://coveralls.io/repos/github/byrnedo/php-nats-streaming/badge.svg?branch=develop)](https://coveralls.io/github/byrnedo/php-nats-streaming?branch=develop) |## Intro
A php client for [Nats Streaming Server](https://nats.io/documentation/streaming/nats-streaming-intro/).
Uses [phpnats](https://github.com/repejota/phpnats) under the hood and closesly resembles it's api.
## Requirements
* php 5.6+
* [stan](https://github.com/nats-io/nats-streaming-server)## Installation
Get [composer](https://getcomposer.org/):
```bash
curl -O http://getcomposer.org/composer.phar && chmod +x composer.phar
```Add php-nats-streaming as a dependency to your project
```bash
php composer.phar require 'byrnedo/php-nats-streaming:^0.2.4'
```## Usage
### Publish
```php
$options = new \NatsStreaming\ConnectionOptions();
$options->setClientID("test");
$options->setClusterID("test-cluster");
$c = new \NatsStreaming\Connection($options);$c->connect();
// Publish
$r = $c->publish('special.subject', 'some serialized payload...');// optionally wait for the ack
$gotAck = $r->wait();
if (!$gotAck) {
...
}$c->close();
```
#### Note
If publishing many messages at a time, you might at first do this:
```php
foreach ($req as $data){
$r = $c->publish(...);
$gotAck = $r->wait();
if (!$gotAck) {
...
}
}
```It's actually *much* faster to do the following:
```php
$rs = [];
foreach ($req as $data){
$rs[] = $c->publish(...);
}foreach ($rs as $r){
$r->wait();
}
```### Subscribe
```php
$options = new \NatsStreaming\ConnectionOptions();
$c = new \NatsStreaming\Connection($options);$c->connect();
$subOptions = new \NatsStreaming\SubscriptionOptions();
$subOptions->setStartAt(\NatsStreamingProtos\StartPosition::First());$sub = $c->subscribe('special.subject', function ($message) {
// implement
}, $subOptions);$sub->wait(1);
// not explicitly needed
$sub->unsubscribe(); // or $sub->close();$c->close();
```
If you want to subscribe to multiple channels you can use `$c->wait()`:
```php
...$c->connect();
...
$sub = $c->subscribe('special.subject', function ($message) {
// implement
}, $subOptions);
$sub2 = $c->subscribe('special.subject', function ($message) {
// implement
}, $subOptions);$c->wait();
```### Queue Group Subscribe
```php
$options = new \NatsStreaming\ConnectionOptions();
$c = new \NatsStreaming\Connection($options);$c->connect();
$subOptions = new \NatsStreaming\SubscriptionOptions();
$sub = $c->queueSubscribe('specialer.subject', 'workgroup', function ($message) {
// implement
}, $subOptions);$sub->wait(1);
// not explicitly needed
$sub->close(); // or $sub->unsubscribe();$c->close();
```
### Manual Ack
```php$options = new \NatsStreaming\ConnectionOptions();
$c = new \NatsStreaming\Connection($options);$c->connect();
$subOptions = new \NatsStreaming\SubscriptionOptions();
$subOptions->setManualAck(true);$sub = $c->subscribe('special.subject', function ($message) {
$message->ack();
}, $subOptions);$sub->wait(1);
$c->close();
```
## License
MIT, see [LICENSE](LICENSE)