PHP Example

From Open Rail Data Wiki
Revision as of 22:25, 26 July 2012 by BarryCarlyon (talk | contribs) (added a non pecl version)
Jump to navigation Jump to search

PECL Stomp Extension Example

Requires PECL STOMP package (just type "pecl install stomp" in the command line).

// Network Rail Stomp Handler example by ian13
$server = "tcp://";
$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);
   if ($con->hasFrame()){
       $msg = $con->readFrame();
       foreach (json_decode($msg->body) as $event) {
         // do stuff with $event here
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:


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

 $con = new Stomp('tcp://');
 $con->clientId = 'someclient';
 $con->connect($username, $password);
 $msg = $con->readFrame();
 if ($msg != null) {
   foreach (json_decode($msg->body) as $event) {
     // do stuff with $event here