GithubHelp home page GithubHelp logo

byrnedo / php-nats-streaming Goto Github PK

View Code? Open in Web Editor NEW
44.0 6.0 21.0 124 KB

Client for nats streaming server in php

License: MIT License

Makefile 0.70% PHP 99.30%
nats-streaming-server php messaging message-queue php71

php-nats-streaming's Introduction

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.

Uses phpnats under the hood and closesly resembles it's api.

Requirements

Installation

Get composer:

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

Add php-nats-streaming as a dependency to your project

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:

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

It's actually much faster to do the following:

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

...

$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

php-nats-streaming's People

Contributors

byrnedo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

php-nats-streaming's Issues

Incorrect behaviour with two consumers on same subject (durable sub)

Heya @byrnedo

So i am creating to consumers on the same subject, they each create their seperate subscription (durable).
I start both. I would expect, that if a message (for that subject) lands in the queue, it is consumed by both.
Only the last client started seems to get the message though. When i restart the client i started first, it gets the message too though.

Cheers,
Nick

Very important setting for performance

After 1.5 weeks of debugging (yes i kind you not) i found the stream socket setting 'tcp_nodelay' to be very important for performance.

See http://php.net/manual/en/context.socket.php

With tcp_nodelay set to false

php-fpm_1                 | [12-Nov-2018 12:37:10] WARNING: [pool www] child 21 said into stderr: "NOTICE: PHP message: 0.12199187278748 :       TLS connecting"
php-fpm_1                 | [12-Nov-2018 12:37:10] WARNING: [pool www] child 21 said into stderr: "NOTICE: PHP message: Subscribing to heartbeat Inbox _INBOX.5be973f68b1f9"
php-fpm_1                 | [12-Nov-2018 12:37:10] WARNING: [pool www] child 21 said into stderr: "NOTICE: PHP message: 0.0010449886322021 :       Preparing data for client connect"
php-fpm_1                 | [12-Nov-2018 12:37:10] WARNING: [pool www] child 21 said into stderr: "NOTICE: PHP message: 8.8930130004883E-5 :       Subscribing for ConnectResponse"
php-fpm_1                 | [12-Nov-2018 12:37:10] WARNING: [pool www] child 21 said into stderr: "NOTICE: PHP message: 0.00017595291137695 :       Auto unscribing after 1 message is received"
php-fpm_1                 | [12-Nov-2018 12:37:10] WARNING: [pool www] child 21 said into stderr: "NOTICE: PHP message: 0.00022983551025391 :       Discovery"
php-fpm_1                 | [12-Nov-2018 12:37:10] WARNING: [pool www] child 21 said into stderr: "NOTICE: PHP message: 0.00027894973754883 :       After Discovery"
php-fpm_1                 | [12-Nov-2018 12:37:10] WARNING: [pool www] child 21 said into stderr: "NOTICE: PHP message: 0.04311203956604 :         Received ConnectResponse"
php-fpm_1                 | [12-Nov-2018 12:37:10] WARNING: [pool www] child 21 said into stderr: "NOTICE: PHP message: 0.04382586479187 :         After wait"
php-fpm_1                 | [12-Nov-2018 12:37:10] WARNING: [pool www] child 21 said into stderr: "NOTICE: PHP message: 0.043874025344849 :         Connect Request"
php-fpm_1                 | [12-Nov-2018 12:37:10] WARNING: [pool www] child 21 said into stderr: "NOTICE: PHP message: 0.16754603385925 :       Connecting"
php-fpm_1                 | [12-Nov-2018 12:37:10] WARNING: [pool www] child 21 said into stderr: "NOTICE: PHP message: 0.0039598941802979 :         Dispatch send request"
php-fpm_1                 | [12-Nov-2018 12:37:10] WARNING: [pool www] child 21 said into stderr: "NOTICE: PHP message: 0.17437696456909 :       Sending email"
php-fpm_1                 | 172.151.0.6 -  12/Nov/2018:12:37:10 +0000 "GET /index.php" 302
php-fpm_1                 | [12-Nov-2018 12:37:10] WARNING: [pool www] child 21 said into stderr: "NOTICE: PHP message: Closing down"
php-fpm_1                 | [12-Nov-2018 12:37:10] WARNING: [pool www] child 21 said into stderr: "NOTICE: PHP message: Closing down"

With tcp_nodelay set to true

php-fpm_1                 | [12-Nov-2018 12:38:57] WARNING: [pool www] child 21 said into stderr: "NOTICE: PHP message: 0.074014902114868 :       TLS connecting"
php-fpm_1                 | [12-Nov-2018 12:38:57] WARNING: [pool www] child 21 said into stderr: "NOTICE: PHP message: Subscribing to heartbeat Inbox _INBOX.5be974612a673"
php-fpm_1                 | [12-Nov-2018 12:38:57] WARNING: [pool www] child 21 said into stderr: "NOTICE: PHP message: 0.00038790702819824 :       Preparing data for client connect"
php-fpm_1                 | [12-Nov-2018 12:38:57] WARNING: [pool www] child 21 said into stderr: "NOTICE: PHP message: 7.319450378418E-5 :       Subscribing for ConnectResponse"
php-fpm_1                 | [12-Nov-2018 12:38:57] WARNING: [pool www] child 21 said into stderr: "NOTICE: PHP message: 0.0001671314239502 :       Auto unscribing after 1 message is received"
php-fpm_1                 | [12-Nov-2018 12:38:57] WARNING: [pool www] child 21 said into stderr: "NOTICE: PHP message: 0.00023007392883301 :       Discovery"
php-fpm_1                 | [12-Nov-2018 12:38:57] WARNING: [pool www] child 21 said into stderr: "NOTICE: PHP message: 0.00029301643371582 :       After Discovery"
php-fpm_1                 | [12-Nov-2018 12:38:57] WARNING: [pool www] child 21 said into stderr: "NOTICE: PHP message: 0.0010581016540527 :         Received ConnectResponse"
php-fpm_1                 | [12-Nov-2018 12:38:57] WARNING: [pool www] child 21 said into stderr: "NOTICE: PHP message: 0.0013542175292969 :         After wait"
php-fpm_1                 | [12-Nov-2018 12:38:57] WARNING: [pool www] child 21 said into stderr: "NOTICE: PHP message: 0.0013811588287354 :         Connect Request"
php-fpm_1                 | [12-Nov-2018 12:38:57] WARNING: [pool www] child 21 said into stderr: "NOTICE: PHP message: 0.076140165328979 :       Connecting"
php-fpm_1                 | [12-Nov-2018 12:38:57] WARNING: [pool www] child 21 said into stderr: "NOTICE: PHP message: 0.0015089511871338 :         Dispatch send request"
php-fpm_1                 | [12-Nov-2018 12:38:57] WARNING: [pool www] child 21 said into stderr: "NOTICE: PHP message: 0.079082012176514 :       Sending email"
php-fpm_1                 | 172.151.0.6 -  12/Nov/2018:12:38:56 +0000 "GET /index.php" 302
php-fpm_1                 | [12-Nov-2018 12:38:57] WARNING: [pool www] child 21 said into stderr: "NOTICE: PHP message: Closing down"
php-fpm_1                 | [12-Nov-2018 12:38:57] WARNING: [pool www] child 21 said into stderr: "NOTICE: PHP message: Closing down"

Cut down response time by ~100MS

phpnats abandoned?

It seems the phpnats package is abandoned. Is there any plan how to solve this?

Cheers

Multiple notices when sending messages

Hi,

When sending messages I've receiving multiple notices

❯ php pub.php 100                                                                                                                                    
PHP Notice:  Undefined index: auth_required in /home/exu/src/kinguin.io/architecture/eventsourcing-nats/php/vendor/repejota/nats/src/Nats/ServerInfo.
php on line 105                                                                                                                                      
PHP Notice:  Undefined index: tls_required in /home/exu/src/kinguin.io/architecture/eventsourcing-nats/php/vendor/repejota/nats/src/Nats/ServerInfo.p
hp on line 106                                                                                                                                       
PHP Notice:  Undefined index: tls_verify in /home/exu/src/kinguin.io/architecture/eventsourcing-nats/php/vendor/repejota/nats/src/Nats/ServerInfo.ph$
 on line 107                                                                                                                                         
Got ACK(0) = 1                                                                                                                                       
Got ACK(1) = 1                                                                                                                                       
...

Does II missing someting ?

and the code.

<?php
require 'vendor/autoload.php';

$options = new \NatsStreaming\ConnectionOptions();
$options->setClientID("test");
$options->setClusterID("test-cluster");

$c = new \NatsStreaming\Connection($options);
$c->connect();

$count = 1;
if (isset($argv, $argv[1])) {
    $count = (int) $argv[1];
}

for ($i = 0; $i < $count; $i++) {
    $r = $c->publish('dupa', json_encode(['nats' => 'example','a'=>rand()]));
    echo "Got ACK($i) = " . $r->wait() . "\n";
}

$c->close();

reconnect to stan(nats streaming) within existing nats connection

hi @byrnedo
i think should be a ability to check(and reconnect) for valid stan connection(nats streaming), not only nats server itself

nats streaming server can close(remove clientId) stan connections using hb_interval, hb_timeout, hb_fail_count
but tcp and nats protocol connection are valid and isConnected() method return true

Durable subscription does not ack message

I might be doing something wrong, but when i try to subscribe to a channel and consume a msg, it seems
the message doesnt get ack'ed properly, when i subscribe again, i still get the same message

$options = new ConnectionOptions();
$options->setClientID("test-consumer");
$client = new Connection($options);
$client->connect();

$subOptions = new SubscriptionOptions();
$subOptions->setStartAt(StartPosition::LastReceived());
$subOptions->setDurableName("testname");

$sub = $client->subscribe('test-channel', function (Msg $message) {
    printf("Data: %s\r\n", $message->getData());
}, $subOptions);



$client->wait(1);

$sub->close();

$client->close();

queue subscription

@byrnedo
I have another question
I try to create queue subscription, but always receive all messages to all subscribers
I don't know it's related to your code or something else
maybe you can help me with that

my code

//subscribe
$natsOpts = new \NatsStreaming\ConnectionOptions();
$natsOpts->setClientID(md5(uniqid((string)microtime(), true)));
$natsOpts->setClusterID($config['nats']['cluster.id']);
$natsOpts->setNatsOptions(new \Nats\ConnectionOptions([
    'host' => $config['nats']['host'],
    'port' => $config['nats']['port'],
    'token' => $config['nats']['auth.token'],
]));
$natsClient = new \NatsStreaming\Connection($natsOpts);
$natsClient->connect();

$subOptions = new \NatsStreaming\SubscriptionOptions();
$subOptions->setDurableName($options['task']);
$subOptions->setManualAck(true);


$natsSub = $natsClient->queueSubscribe(
    $options['task'],
    $options['task'],
    $callback,
    $subOptions
);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.