Skip to content

brettmc/capablue-stomp

Repository files navigation

** Notice: capablue/stomp disappeared from github around 2018-09-25. I had a local copy of the latest tagged version, 0.3.1, which I'm resurrecting here for anybody to use. I don't intent to maintain it, though, and suggest that projects dependent on capablue/stomp switch over to react/stomp. The lack of heart-beating in react/stomp will need to be addressed in some fashion. **

React/STOMP

STOMP bindings for React.

STOMP is a messaging protocol. It is supported by most message queue brokers, such as RabbitMQ, Apollo and many others.

A message queue is used for asynchronous inter-process communication. This can be useful for workers, general long-running tasks, or communication between long-running daemons.

Build Status

Install

The recommended way to install react/stomp is through composer.

{
    "require": {
        "react/stomp": "0.1.*"
    }
}

Example

You can interact with a STOMP server by using the React\Stomp\Client.

<?php

$loop = React\EventLoop\Factory::create();
$factory = new React\Stomp\Factory($loop);
$client = $factory->createClient(array('vhost' => '/', 'login' => 'guest', 'passcode' => 'guest'));

$client
    ->connect()
    ->then(function ($client) use ($loop) {
        $client->subscribe('/topic/foo', function ($frame) {
            echo "Message received: {$frame->body}\n";
        });

        $loop->addPeriodicTimer(1, function () use ($client) {
            $client->send('/topic/foo', 'le message');
        });
    });

$loop->run();

Options

  • host: Host to connect to, defaults to 127.0.0.1.
  • port: Port to connect to, defaults to 61613 (rabbitmq's stomp plugin).
  • vhost: Virtual host, defaults to /.
  • login: Login user name, defaults to guest.
  • passcode: Login passcode, defaults to guest.
  • protocol: tcp or tls, defaults to tcp.
  • timeout: socket timeout, defaults to 0.

Acknowledgement

When subscribing with the subscribe method, messages are considered acknowledged as soon as they are sent by the server (ack header is set to 'auto').

You can subscribe with a manual acknowledgement by using subscribeWithAck (see SUBSCRIBE in the STOMP spec for available ack mode values).

You will get a React\Stomp\AckResolver as second argument of the callback to acknowledge or not the message :

$client->subscribeWithAck('/topic/foo', 'client', function ($frame, $ackResolver) {
    if ($problem) {
        $ackResolver->nack();
    } else {
        $ackResolver->ack();
    }
});

Heart-beating

Heart-beat intervals can be defined before connection :

$client = $factory->createClient();
$client->setHeartBeat(new HeartBeat(1,0.5,2));
$client->on('no_heartbeat', function() use($client, $loop) {
    error_log("Heart-beating is not supported.");
});
$client->on('cardiac_arrest', function() use($client, $loop) {
    $client->disconnect();
    $loop->tick();
    $client->output->close();
    throw new \RuntimeException("No heart-beats received from the queue broker")
});

Where we ask the broker to send us heart-beats every second, with another half-second threshold, and we promise to send a heart-beat every 2 seconds.

The broker CAN increase intervals, or reset it to 0, if no heart-beating supported. In this case no_heartbeat event emitted. It does not disconnect automatically, as it may be acceptable to continue without heart-beating.

If no heart-beats received within interval + threshold, the cardiac_arrest event emitted. It is strongly recommended to finish all the jobs and disconnect, yet it does not disconnect automatically. No heart-beats expected after 'cardiac_arrest'.

Todo

  • Support nul bytes in frame body
  • Consuming ACKs
  • Transactions
  • Streaming frame bodies (using stream API)

Tests

To run the test suite, you need PHPUnit.

$ phpunit

License

MIT, see LICENSE.

Resources