trainjunkies-packages / data-feeds-queue-subscriber
Subscribe to Network Rail & National Rail data feed queues
Requires
- php: ^7.4.24
- ext-json: *
- stomp-php/stomp-php: ^5.1.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.31
- phpspec/phpspec: *
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^9.0
- squizlabs/php_codesniffer: ^3.4
- vlucas/phpdotenv: ^5.6
README
PHP package to consume Open Rail Data feeds from National Rail & Network Rail via Active MQ STOMP. Supports durable connections with server heartbeats.
Installation
via Composer
Install Composer and require the package with the below command.
composer require third-rail-packages/data-feeds-queue-subscriber
Getting Started
National Rail - DARWIN PushPort
Signup to the National Rail Open Data feeds to generate your unique credentials. More information can be found on the Open Rail Data Wiki.
./scripts/national-rail
Network Rail - NROD, TRUST, TD
Signup to the Network Rail Open Data platform to generate a username and password for the feeds. Be sure to activate the Active MQ topics you wish to subscribe to. More information can be found on the Open Rail Data Wiki
Example code can be found in ./scripts/network-rail
Durable Subscription with heartbeats
Below example with request a durable connection with supplied Client ID and ActiveMQ subscription name expecting a heartbeat frame from the server every 20 seconds while emitting one every 5.
<?php // ./trust-durable-example.php use ThirdRailPackages\QueueSubscriber\NetworkRail\Topics; use ThirdRailPackages\QueueSubscriber\Stomp\DurableSubscription; use ThirdRailPackages\QueueSubscriber\Stomp\Message; use ThirdRailPackages\QueueSubscriber\Stomp\StompClientFactory; include __DIR__ . '/vendor/autoload.php'; try { // Additional arguments can be useed to configure Stomp connection $client = StompClientFactory::make( 'publicdatafeeds.networkrail.co.uk', 61618, 'mynrod-email@example.com', 'S3eC7et' ); // Consult Open Rail Data wiki on Best practice for durable subscriptions return new DurableSubscription( $client, 'my-nrod-production' ); $subscription->consume(Topics\Trust::MOVEMENT_ALL, function(Message $message) { echo PHP_EOL; var_dump(json_decode($message->getBody(), true)); echo PHP_EOL; }); } catch (\Exception $e) { echo $e->getMessage() . PHP_EOL; // Place sleep(X) here or implement exponential backoff exit(1); }
Development
Copy and complete .env.dist
to .env
with desired feed credentials.
The development environment and dependencies are managed with Docker. In the same directory as the checked out cloned repository, run the below command to start the Docker Compose environment.
docker-compose up -d --build
Login to the app
container begin development.
docker-compose run --rm app sh
Example scripts can be executed inside.
php scripts/network-rail/trust.php
Xdebug has also been installed to debug PHP.
An ActiveMQ container is also present to aid development / debugging. The management interface can be accessed through a browser on http://localhost:8181/admin
with the credentials admin
& admin
.
Authors
- Ben McManus - bennoislost
See also the list of contributors who participated in this project
License
This project is licensed under the MIT License - see the LICENSE.md file for details