Skip to content

Durable Subscribers

Jens Radtke edited this page Sep 6, 2015 · 3 revisions

Durable topic subscribers are not part of the Stomp protocol, but an ActiveMQ feature that allow topic subscribers to receive messages sent to the topic while there were offline.

Stomp protocol implementation in ActiveMQ enables this feature for Stomp subscribers as well. To use this feature, first you need to define a client id your consumer will use, like this

$consumer = new Stomp('tcp://localhost:61613');
$consumer->clientId = 'test';

Next, you client need to provide this id while subscribing to the topic. This is done by passing special activemq.subcriptionName header along with the SUBSCRIBE frame. But once, you've set the client id to your consumer, PHP Stomp client will do this work for you. You only need to indicate that your topic subscription is durable by setting the durable flag to true.

$consumer->subscribe('/topic/test', null, true, true);

If you want to end your durable subscription you need to call unsubscribe with active durable flag again. If you don't set the durable flag ActiveMQ keeps your durable subscription, so you would still receive all messages that have been sent to this topic. Note that there is a difference between Apache ActiveMQ and Apache Apollo.

if ($consumer->getProtocol() instanceof \Stomp\Protocol\ActiveMq) {
    // ActiveMq
    $consumer->unsubscribe('/topic/test');
    $consumer->unsubscribe('/topic/test', null, true, true);
} else {
    // Apollo
    $consumer->unsubscribe('/topic/test', null, true, true);
}

Persistent Messages

This chapter needs a review and a new example.

One more important thing is that you need to send persistent messages to the topic in you want them delivered to the delivered to the durable consumers that are currently offline. You can do that by passing a persistent header along with your messages

$producer->send('/topic/test', 'test1', array('persistent' => 'true'));

The full example of the PHP Stomp client durable subscribers can be found in examples/durable.php.