repejota / phpnats

A PHP client for the NATSio cloud messaging system.

Home Page:http://nats.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Issue subscribing to an event

EwanValentine opened this issue · comments

Hi there,

I have a Go application which fires an event off to a NATS server, listening to that NATS server is a PHP application, which has an event listener for that event within a Laravel service provider.

<?php

namespace SixtyFiveContrib\Providers;

use SixtyFiveContrib\Jobs\CreateSubmissionMetaDataJob;

use Nats\Connection;
use Nats\ConnectionOptions as Options;

use Illuminate\Support\ServiceProvider;
use Illuminate\Foundation\Bus\DispatchesCommands;
use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;

/**
 * NatsServiceProvider
 *
 * Registers a NATS connections, listens for NATS 
 * events and converts them into native Laravel events.
 *
 * @author    Ewan Valentine <ewan@theladbible.com>
 * @copyright The LAD bible Group - 2016
 */
class NatsServiceProvider extends ServiceProvider 
{
    use DispatchesCommands;

    /**
     * boot
     *
     * @param DispatcherContract $events
     */
    public function boot() 
    {
        parent::boot();

        $nats = $this->app['Nats'];

        $nats->subscribe('submissions.created', function($submission) {

            \Log::info("submissions.created event");

            $submission = json_decode($submission);
            $this->dispatch(new CreateSubmissionMetaDataJob($submission));
        });

        $nats->subscribe('submissions.agreed', function($agreement) use($nats) {

            // { "id": 123, "agree": true }
            $agreement = json_decode($agreement);
            $this->dispatch(new AgreeSubmissionJob($agreement, $nats));
        });

        return $this->app;
    }

    /**
     * register
     */
    public function register() 
    {
        // Register a nats connection as a service
        $this->app->singleton("Nats", function() {

            // Make configurable
            $options = new Options();

            $options->setHost(env('NATS_HOST', 'localhost'));

            $client = new Connection($options);
            $client->connect();
            return $client;
        });
    }
}

I notice in my docker logs, NATS receives this event...

nats_1            | [1] 2016/08/05 12:24:04.322858 [TRC] 172.19.0.11:54200 - cid:17 - ->> [PUB submissions.created 6072]
nats_1            | [1] 2016/08/05 12:24:04.322916 [TRC] 172.19.0.11:54200 - cid:17 - ->> MSG_PAYLOAD: [{"id":"579b2e15b315a00001f4e295","facebook_id":"1234","status":{"name":"awaiting_agreement"},"submission":{"title":"","story":"This is a test","author":"Ewan Valentine","email":"","address":"","dob":"","phone":"","site":"","legal":"","agreed":false,"signature":"","files":[{"filename":"GLrKvxALnivWfHpC.png?_nc_ad=z-m\u0026oh=ab190d23d51b2132957d8fe95f3aaffd\u0026oe=57A6ABD4","filesize":0,"filetype":""}],"agreement":{"id":"","facebook_id":""}},"log":[{"updated_at":"2016-07-29T11:20:45.836Z","status":{"name":"option_one"}},{"updated_at":"2016-07-29T11:24:04.566Z","status":{"name":"option_one"}},{"updated_at":"2016-08-01T15:41:11.811Z","status":{"name":"option_one"}},{"updated_at":"2016-08-01T15:41:28.597Z","status":{"name":"option_one"}},{"updated_at":"2016-08-01T15:41:33.778Z","status":{"name":"option_one"}},{"updated_at":"2016-08-02T10:52:22.788Z","status":{"name":"option_one"}},{"updated_at":"2016-08-03T09:51:51.5Z","status":{"name":"awaiting_file"}},{"updated_at":"2016-08-03T09:51:58.666Z","status":{"name":"menu"}},{"updated_at":"2016-08-03T11:05:13.145Z","status":{"name":"awaiting_file"}},{"updated_at":"2016-08-03T11:05:17.938Z","status":{"name":"menu"}},{"updated_at":"2016-08-03T11:05:27.92Z","status":{"name":"awaiting_file"}},{"updated_at":"2016-08-03T14:11:53.683Z","status":{"name":"awaiting_agreement"}},{"updated_at":"2016-08-03T14:11:59.102Z","status":{"name":"menu"}},{"updated_at":"2016-08-03T14:24:18.13Z","status":{"name":"awaiting_file"}},{"updated_at":"2016-08-03T14:24:22.634Z","status":{"name":"menu"}},{"updated_at":"2016-08-03T14:29:59.544Z","status":{"name":"awaiting_file"}},{"updated_at":"2016-08-03T14:30:04.716Z","status":{"name":"menu"}},{"updated_at":"2016-08-03T14:30:10.693Z","status":{"name":"awaiting_file"}},{"updated_at":"2016-08-03T16:27:00.608Z","status":{"name":"awiating_description"}},{"updated_at":"2016-08-03T16:27:04.936Z","status":{"name":"menu"}},{"updated_at":"2016-08-03T16:27:14.239Z","status":{"name":"awaiting_file"}},{"updated_at":"2016-08-03T16:34:45.209Z","status":{"name":"awiating_description"}},{"updated_at":"2016-08-03T16:35:01.73Z","status":{"name":"menu"}},{"updated_at":"2016-08-03T17:00:29.249Z","status":{"name":"awaiting_file"}},{"updated_at":"2016-08-03T17:27:37.78Z","status":{"name":"awiating_description"}},{"updated_at":"2016-08-03T17:27:40.773Z","status":{"name":"menu"}},{"updated_at":"2016-08-03T17:27:47.182Z","status":{"name":"awaiting_file"}},{"updated_at":"2016-08-03T17:39:50.943Z","status":{"name":"awiating_description"}},{"updated_at":"2016-08-03T17:39:54.924Z","status":{"name":"menu"}},{"updated_at":"2016-08-03T17:40:01.068Z","status":{"name":"awaiting_file"}},{"updated_at":"2016-08-03T17:43:46.809Z","status":{"name":"awaiting_description"}},{"updated_at":"2016-08-03T17:43:50.3Z","status":{"name":"menu"}},{"updated_at":"2016-08-03T17:43:57.65Z","status":{"name":"awaiting_file"}},{"updated_at":"2016-08-03T17:44:00.749Z","status":{"name":"awaiting_description"}},{"updated_at":"2016-08-04T14:22:21.889Z","status":{"name":"awaiting_agreement"}},{"updated_at":"2016-08-04T14:22:30.289Z","status":{"name":"awaiting_file"}},{"updated_at":"2016-08-04T14:22:41.073Z","status":{"name":"awaiting_description"}},{"updated_at":"2016-08-04T14:26:28.479Z","status":{"name":"awaiting_agreement"}},{"updated_at":"2016-08-04T14:26:30.965Z","status":{"name":"menu"}},{"updated_at":"2016-08-04T14:26:36.893Z","status":{"name":"awaiting_file"}},{"updated_at":"2016-08-04T14:26:48.352Z","status":{"name":"awaiting_description"}},{"updated_at":"2016-08-04T14:28:48.456Z","status":{"name":"awaiting_agreement"}},{"updated_at":"2016-08-04T14:28:51.077Z","status":{"name":"menu"}},{"updated_at":"2016-08-04T14:28:57.18Z","status":{"name":"awaiting_file"}},{"updated_at":"2016-08-04T14:29:16.278Z","status":{"name":"awaiting_description"}},{"updated_at":"2016-08-05T11:32:39.746Z","status":{"name":"awaiting_agreement"}},{"updated_at":"2016-08-05T11:32:46.843Z","status":{"name":"awaiting_file"}},{"updated_at":"2016-08-05T11:32:55.817Z","status":{"name":"awaiting_description"}},{"updated_at":"2016-08-05T11:35:13.817Z","status":{"name":"awaiting_agreement"}},{"updated_at":"2016-08-05T11:35:23.685Z","status":{"name":"awaiting_file"}},{"updated_at":"2016-08-05T11:35:30.294Z","status":{"name":"awaiting_description"}},{"updated_at":"2016-08-05T11:47:28.114Z","status":{"name":"awaiting_agreement"}},{"updated_at":"2016-08-05T11:49:13.527Z","status":{"name":"awaiting_file"}},{"updated_at":"2016-08-05T11:49:16.827Z","status":{"name":"menu"}},{"updated_at":"2016-08-05T11:49:38.645Z","status":{"name":"awaiting_file"}},{"updated_at":"2016-08-05T11:49:48.696Z","status":{"name":"awaiting_description"}},{"updated_at":"2016-08-05T11:51:04.094Z","status":{"name":"awaiting_agreement"}},{"updated_at":"2016-08-05T11:51:13.411Z","status":{"name":"awaiting_file"}},{"updated_at":"2016-08-05T11:51:24.15Z","status":{"name":"awaiting_description"}},{"updated_at":"2016-08-05T11:53:08.858Z","status":{"name":"awaiting_agreement"}},{"updated_at":"2016-08-05T11:53:19.515Z","status":{"name":"awaiting_file"}},{"updated_at":"2016-08-05T11:53:27.388Z","status":{"name":"menu"}},{"updated_at":"2016-08-05T11:53:36.252Z","status":{"name":"awaiting_file"}},{"updated_at":"2016-08-05T11:53:42.728Z","status":{"name":"awaiting_description"}},{"updated_at":"2016-08-05T11:55:39.866Z","status":{"name":"awaiting_agreement"}},{"updated_at":"2016-08-05T11:57:31.134Z","status":{"name":"menu"}},{"updated_at":"2016-08-05T11:57:37.622Z","status":{"name":"menu"}},{"updated_at":"2016-08-05T11:57:50.094Z","status":{"name":"awaiting_file"}},{"updated_at":"2016-08-05T11:57:57.964Z","status":{"name":"awaiting_description"}},{"updated_at":"2016-08-05T12:00:56.46Z","status":{"name":"awaiting_agreement"}},{"updated_at":"2016-08-05T12:01:04.556Z","status":{"name":"menu"}},{"updated_at":"2016-08-05T12:22:45.578Z","status":{"name":"awaiting_file"}},{"updated_at":"2016-08-05T12:23:23.843Z","status":{"name":"awaiting_file"}}],"created_at":"2016-07-29T10:21:09.519Z","updated_at":"2016-08-05T12:23:23.843Z"}]

It's quite a large json string, could that be the issue maybe?

I created a test route in my Laravel application...

get('test', function() {
    $nats = App::make('Nats');
    $nats->publish("submissions.created", json_encode([
        "title"     => "This is a test title",
        "story"     => "This is a test story",
        "author"    => "Ewan Valentine",
        "email"     => "my@email.com",
        "address"   => "",
        "dob"       => "02/07/1989",
        "phone"     => "01332 331331",
        "site"      => "The LAD bible",
        "legal"     => "The LAD bible - Facebook",
        "agreed"    => true,
        "signature" => "E. Valentine",
        "files"     => [
            [
                "filename" => "test.jpg",
                "filesize" => "123534",
                "filetype" => "image",
            ],
            [
                "filename" => "test-2.mp4",
                "filesize" => "456456", 
                "filetype" => "video",
            ],
        ]
    ]));
    $nats->wait(1);

    return "Done!";
});

That works when I include the $nats->wait(1); but doesn't when I omit that line, however there's no wait in the Go sdk.

Cheers in advance!

HI @EwanValentine the size of your json is not the issue, we have more massive payload without problem.

You need to call wait, with or without number of message, without this the PHP client does not listen the nats server and just exist. If you pass a number of message, the wait loop will exit after this number of processed message, so you need to take care of starting the script again (we use docker or supervisord to solve this). It's a best practive to exit after a few job as PHP and even more with complex framework is really "memory leaky"

Very good point! I was switching from a Go codebase where I had concurrency back to a PHP codebase haha.

I've fixed this by creating a command line NATS listener.

Thanks :)