2017 © Pedro Peláez
 

library php-nats-streaming

PHP Client for nats-streaming-server.

image

byrnedo/php-nats-streaming

PHP Client for nats-streaming-server.

  • Monday, December 4, 2017
  • by byrnedo
  • Repository
  • 4 Watchers
  • 10 Stars
  • 3,791 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 3 Open issues
  • 12 Versions
  • 66 % Grown

The README.md

PHP Nats Streaming Server Client

Build

Master Develop
Build Status Build Status

Coverage

Master Develop
Coverage Status Coverage Status

Intro

A php client for Nats Streaming Server., (*1)

Uses phpnats under the hood and closesly resembles it's api., (*2)

Requirements

Installation

Get composer:, (*3)

curl -O http://getcomposer.org/composer.phar && chmod +x composer.phar

Add php-nats-streaming as a dependency to your project, (*4)

php composer.phar require 'byrnedo/php-nats-streaming:^0.2.4'

Usage

Publish

$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:, (*5)

foreach ($req as $data){
    $r = $c->publish(...);
    $gotAck = $r->wait();
    if (!$gotAck) {
        ...
    }
}

It's actually much faster to do the following:, (*6)

$rs = [];
foreach ($req as $data){
    $rs[] = $c->publish(...);
}

foreach ($rs as $r){
    $r->wait();
}

Subscribe

$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():, (*7)

...

$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

$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


$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, (*8)

The Versions

04/12 2017
04/12 2017
21/11 2017

dev-feature/payload-stashing

dev-feature/payload-stashing

PHP Client for nats-streaming-server.

  Sources   Download

MIT

The Requires

 

The Development Requires

18/11 2017

dev-feature/improve-ack

dev-feature/improve-ack

PHP Client for nats-streaming-server.

  Sources   Download

MIT

The Requires

 

The Development Requires