PHP Example: Difference between revisions
BarryCarlyon (talk | contribs) m mixed random new line in the middle |
BarryCarlyon (talk | contribs) added a non pecl version |
||
Line 1: | Line 1: | ||
==PECL Stomp Extension Example== | |||
Requires [http://pecl.php.net/package/stomp PECL STOMP] package (just type "pecl install stomp" in the command line). | |||
<code> | <code> | ||
<?php | <?php | ||
Line 25: | Line 30: | ||
</code> | </code> | ||
You can additionally use a client-id header. | |||
This means if you disconnect, when you reconnect, the server should resend the messages that you missed whilst offline. | |||
<code> | |||
$con = new Stomp($server, $user, $password, array('client-id' => 'somename'); | |||
</code> | |||
Further Documentation: http://www.php.net/manual/en/class.stomp.php | |||
===STOMP LIbrary=== | |||
The Stomp Client from [http://stomp.fusesource.org/download.html Fuse Source]. It has some minor issues, it injects spaces in the username/password and sleeps on message retrieval. | |||
An adjusted version can be grabbed 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> | |||
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(); | |||
</code> |
Revision as of 22:25, 26 July 2012
PECL Stomp Extension Example
Requires PECL STOMP package (just type "pecl install stomp" in the command line).
<?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());
?>
You can additionally use a client-id header.
This means if you disconnect, when you reconnect, the server should resend the messages that you missed whilst offline.
$con = new Stomp($server, $user, $password, array('client-id' => 'somename');
Further Documentation: http://www.php.net/manual/en/class.stomp.php
STOMP LIbrary
The Stomp Client from Fuse Source. It has some minor issues, it injects spaces in the username/password and sleeps on message retrieval. An adjusted version can be grabbed 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();