PHP Example: Difference between revisions
BarryCarlyon (talk | contribs) Date Notes |
PeterHicks (talk | contribs) |
||
Line 1: | Line 1: | ||
==PECL Stomp | ==PECL Stomp extension == | ||
This example requires [http://pecl.php.net/package/stomp PECL STOMP] package. To install, type "pecl install stomp". | |||
<code> | <code> | ||
Line 30: | Line 29: | ||
</code> | </code> | ||
Optionally, you can specify a client-id header, which will ensure message persistence (if enabled on the server) and queue messages whilst your subscriber is offline. | |||
<code> | <code> | ||
$con = new Stomp($server, $user, $password, array('client-id' => 'somename'); | $con = new Stomp($server, $user, $password, array('client-id' => 'somename'); | ||
</code> | </code> | ||
Further | Further documentation on the PHP Stomp library is available at [http://www.php.net/manual/en/class.stomp.php http://www.php.net/manual/en/class.stomp.php] | ||
===Stomp library=== | |||
An alternative Stomp client is available from [http://stomp.fusesource.org/download.html Fuse Source]. It has some minor issues, such as injecting spaces in the username/password, and sleeping on message retrieval. | |||
A fixed version can be obtained from [http://mail-archives.apache.org/mod_mbox/activemq-users/201103.mbox/%3CAANLkTinetbR6w-CFzJ+q-Xk3p1xkkr3BzYBcxDAU8VPO@mail.gmail.com%3E here] or [http://mail-archives.apache.org/mod_mbox/activemq-users/201103.mbox/raw/%3CAANLkTinetbR6w-CFzJ+q-Xk3p1xkkr3BzYBcxDAU8VPO@mail.gmail.com%3E here]. | |||
<code> | <code> | ||
Line 64: | Line 62: | ||
$con->disconnect(); | $con->disconnect(); | ||
</code> | </code> | ||
Revision as of 20:50, 31 July 2012
PECL Stomp extension
This example requires PECL STOMP package. To install, type "pecl install stomp".
<?php
// Network Rail Stomp Handler example by ian13
$server = "tcp://datafeeds.networkrail.co.uk:61618";
$user = "username";
$password = "password";
$channel = "TRAIN_MVT_ALL_TOC";
$con = new Stomp($server, $user, $password);
if (!$con) {
die('Connection failed: ' . stomp_connect_error());
}
$con->subscribe("/topic/" . $channel);
while($con){
if ($con->hasFrame()){
$msg = $con->readFrame();
foreach (json_decode($msg->body) as $event) {
// do stuff with $event here
}
$con->ack($msg);
}
}
die('Connection lost: ' . time());
?>
Optionally, you can specify a client-id header, which will ensure message persistence (if enabled on the server) and queue messages whilst your subscriber is offline.
$con = new Stomp($server, $user, $password, array('client-id' => 'somename');
Further documentation on the PHP Stomp library is available at http://www.php.net/manual/en/class.stomp.php
Stomp library
An alternative Stomp client is available from Fuse Source. It has some minor issues, such as injecting spaces in the username/password, and sleeping on message retrieval.
A fixed version can be obtained from here or here.
require_once('Stomp.php');
$con = new Stomp('tcp://datafeeds.networkrail.co.uk:61618/');
$con->clientId = 'someclient';
$con->connect($username, $password);
$con->subscribe("/topic/TRAIN_MVT_ALL_TOC");
$msg = $con->readFrame();
if ($msg != null) {
foreach (json_decode($msg->body) as $event) {
// do stuff with $event here
}
$con->ack(msg);
}
$con->disconnect();