ok Im up and running but failing

88 views
Skip to first unread message

Jonny

unread,
Jul 31, 2023, 12:49:42 AMJul 31
to A gathering place for the Open Rail Data community
I managed to setup a vps and stomp client on plesk. Took me some time but got there.

I have managed to get some code together however it connects to server and subscribes to the topic ok, however I am unable to read any messages. Can anyone assist? here is my code so far:

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

// Check if the Stomp extension is installed
if (!extension_loaded('stomp')) {
    die('Stomp extension not installed. Please install and enable it to proceed.');
}

// Network Rail Stomp Handler example by ian13
$server = "tcp://datafeeds.networkrail.co.uk:61618";
$user = "*****@******.co.uk"; // removed for obvious reasons
$password = "************"; // removed for obvious reasons
$channel = "RTPPM_ALL";

try {
    // Create a new Stomp connection
    $stomp = new Stomp($server, $user, $password);

    // Check if the connection was successful
    if (!$stomp) {
        die('Connection failed: ' . stomp_connect_error());
    } else {
        echo "<p>Connected</p>";
    }

    // Subscribe to the topic
    //$con->subscribe("/topic/" . $channel);
    $stomp->subscribe("/topic/".$channel);

    // Check if the subscription was successful
    if (!$stomp) {
        die('Subscription failed: ' . stomp_connect_error());
    } else {
        echo "<p>Subscribed to /topic/{$channel}</p>";
    }

    // Receive a message from the topic
$msg = $stomp->readFrame();
var_dump($msg);

    // Do what you want with the message
    if ($msg != null) {
        echo "<p>Received message with body '$msg->body'</p>";
        // Mark the message as received in the queue
        $stomp->ack($msg);
    } else {
        echo "<p>Failed to receive a message</p>";
    }

    // Disconnect
    unset($stomp);
    echo "<p>Connection closed</p>";
} catch (StompException $e) {
    // Catch any Stomp-related exceptions
    die('Stomp Exception: ' . $e->getMessage());
} catch (Exception $e) {
    // Catch any other exceptions
    die('Exception: ' . $e->getMessage());
}
?>

Matthew Burdett

unread,
Jul 31, 2023, 1:38:46 AMJul 31
to openrail...@googlegroups.com
Pretty sure you're supposed to have a while loop on the connection to keep it running, as RTPPM messages are only sent once a minute correct me if I'm wrong

Have another look here you can see on the first example while($con){ which presumably keeps it connected so messages can be received 


Have you also installed something like ActiveMQ? Looking at your example you're also using data feeds not publicdatafeeds - I'm sure that's not a problem as I think a redirect is in place but worth changing anyway...

--
You received this message because you are subscribed to the Google Groups "A gathering place for the Open Rail Data community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to openraildata-t...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/openraildata-talk/6ebe13f4-b5be-4de0-b4b9-e9aaeffa83d1n%40googlegroups.com.

Jonny

unread,
Jul 31, 2023, 8:02:33 AMJul 31
to A gathering place for the Open Rail Data community
ok, So after some editing I have the following code. However the code timesout with a 504, I have increased the PHP timeouts to 90 seconds or so.

The code executes fine if I comment out the while loop. but I get the 504 with the while loop.

Can anyone offer some assistance where my code may be wrong? All I want to do is see the message lol. Here is my code.

<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

// Check if the Stomp extension is installed
if (!extension_loaded('stomp')) {
    die('Stomp extension not installed. Please install and enable it to proceed.');
}

// Network Rail Stomp Handler example by ian13
$server = "tcp://publicdatafeeds.networkrail.co.uk:61618";
$user = "****";
$password = "****";
$channel = "/topic/RTPPM_ALL";


 try {
    // Create a new Stomp connection
    $stomp = new Stomp($server, $user, $password);

    // Check if the connection was successful
    if (!$stomp) {
        echo "<p>Failed to connect</p>";

        die('Connection failed: ' . stomp_connect_error());
    } else {
        echo "<p>Connected</p>";
    }


$isSubscribe = $stomp->subscribe($channel);


 // Check if the subscription was successful
    if (!$isSubscribe) {

        die('Subscription failed: ' . stomp_connect_error());
        echo "<p>Failed to Subscribe</p>";

    } else {
        echo "<p>Subscribed to /topic/{$channel}</p>";
    }

// start of while loop
while($isSubscribe){
    if ($stomp->hasFrame()) {
        $frame = $stomp->readFrame();
        if ($frame != NULL) {
            print "Received: " . $frame->body . " - at - " . date("Y-m-d H:i:s"). "\n";
            $stomp->ack($frame);
        }
       sleep(1);
    }
    else {
        print "No frames to read\n";
    }
}
// end of while loop


if($isSubscribe){
    $stomp->unsubscribe($channel);
    echo "<p>Now Unsubscribed</p>";
    unset($stomp);
    echo "<p>Now Unset</p>";
}


} catch(StompException $e) {
    die('Connection failed: ' . $e->getMessage());

} catch (Exception $e) {
    // Catch any other exceptions
    die('Exception: ' . $e->getMessage());
}
?>

Matthew B

unread,
Jul 31, 2023, 8:53:17 AMJul 31
to A gathering place for the Open Rail Data community
Can you try running this in a new file? I changed your original code below and it worked fine for me...
The below will put the contents of the RTPPM into a file under /home assuming you're using linux

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

// Check if the Stomp extension is installed
if (!extension_loaded('stomp')) {
    die('Stomp extension not installed. Please install and enable it to proceed.');
}

// Network Rail Stomp Handler example by ian13
$server = "tcp://datafeeds.networkrail.co.uk:61618";
$user = ""; // removed for obvious reasons
$password = ""; // removed for obvious reasons
$channel = "RTPPM_ALL";

try {

    $stomp = new Stomp($server, $user, $password);
    if (!$stomp) {
        echo "<p>Error connecting to stomp</p>";
    }
$stomp->subscribe("/topic/".$channel);
while($stomp){
                if ($stomp->hasFrame()){
                        $msg = $stomp->readFrame();
if($msg != null){
//var_dump($msg);
                                $response = print_r($msg, true);
file_put_contents('/home/rtppm'.date("y-m-d Hi", time()).'.json', $response, FILE_APPEND);
$stomp->ack($msg);

}
}
}
    // Disconnect
    unset($stomp);
    echo "<p>Connection closed</p>";
} catch (StompException $e) {
    die('Stomp Exception: ' . $e->getMessage());
} catch (Exception $e) {
    die('Exception: ' . $e->getMessage());
}
?>

Jonny

unread,
Jul 31, 2023, 2:46:55 PMJul 31
to A gathering place for the Open Rail Data community
Ok, so I uploaded your version as a new file and it still gets a 504 gateway error. I had to change the 

file_put_contents line to:
file_put_contents(date("y-m-d Hi", time()).'.json', $response, FILE_APPEND);

as I the folder my stomp php script currently resides in is an httpdoc folder.

I am new to vps etc. Is there anything obvious that I could be doing wroing?

I installed stomp and it connects fine when I tested it. I just cant get a mesage to appear.

any help would be much appreciated.

Peter Hicks (Poggs)

unread,
Jul 31, 2023, 2:48:49 PMJul 31
to A gathering place for the Open Rail Data community
Hi Jonny

> On 31 Jul 2023, at 19:46, Jonny <jonnythe...@gmail.com> wrote:
>
> I installed stomp and it connects fine when I tested it. I just cant get a mesage to appear.

If you’re getting a 504 error, it sounds like you’re trying to call the PHP script via a web page. This isn’t how real-time streams of data work - you need a long-lived process consuming them and doing something with them (e.g. storing data in a database). If you’re just executing PHP through your web server, you will be in for a world of pain.

Try running the PHP script at the command line instead.


Peter

Matthew Burdett

unread,
Jul 31, 2023, 2:53:51 PMJul 31
to openrail...@googlegroups.com
To keep it running after closing the SSH console you can maybe try 

 nohup php <your script path> &

The & indicates running it as a background task.

To kill it you can use htop (sudo apt-get install htop) then use the arrow keys, F9 on the script to kill it. 

--
You received this message because you are subscribed to the Google Groups "A gathering place for the Open Rail Data community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to openraildata-t...@googlegroups.com.

Jonny

unread,
Jul 31, 2023, 3:45:41 PMJul 31
to A gathering place for the Open Rail Data community
Thank you both, I ahve used a CRON job to run the file. I assume thats ok?

It has thrown a strange anomally where it will keep running the file, even if I change the following line:
file_put_contents(date("y-m-d Hi", time()).'.json', $response, FILE_APPEND);
to
file_put_contents("Test: ".date("y-m-d Hi", time()).'.json', $response, FILE_APPEND);

it will still produce files with the original file name format and the second one.

I restarted the server and hopefully bugs are gone.

Thank you both again

Matthew Burdett

unread,
Jul 31, 2023, 3:51:01 PMJul 31
to openrail...@googlegroups.com
You've probably got two connections running then one with the old file and one with the new. I don't know how you kill something run from cron - I assume it's doable via htop.

Better to look at using nohup (a service daemon might be a bit more challenging) so you can execute it to background, cron is a scheduler and you only want the file to run once.

Reply all
Reply to author
Forward
0 new messages