Node JS Examples

From Open Rail Data Wiki
Jump to navigation Jump to search


Remember to subscribe to the feed(s) you're using via the web interface!

var prettyjson = require('prettyjson'),
    StompClient = require('stomp-client').StompClient;

var destination = '/topic/TRAIN_MVT_ALL_TOC',
    client = new StompClient('', 61618, 'emailaddress', 'your_password', '1.0');

client.connect(function(sessionId) {
    console.log('Trying to connect...');
    client.subscribe(destination, function(body, headers) {

node.js (Stompit)

Using stompit library from

var stompit = require("stompit");
var async = require("async");

// Connect options with standard headers
var connectOptions = {
    "host": "",
    "port": 61618,
    "connectHeaders": {
        "heart-beat": "15000,15000",// hear-beat of 15 seconds
        "client-id": "",            // request a durable subscription - set this to the login name you use to subscribe
        "host": "/",
        "login": "",                // your username
        "passcode": ""              // your password

// Reconnect management for stompit client
var reconnectOptions = {
    "initialReconnectDelay": 10,    // milliseconds delay of the first reconnect
    "maxReconnectDelay": 30000,     // maximum milliseconds delay of any reconnect
    "useExponentialBackOff": true,  // exponential increase in reconnect delay
    "maxReconnects": 30,            // maximum number of failed reconnects consecutively
    "randomize": false              // randomly choose a server to use when reconnecting
                                    // (there are no other servers at this time)    

var connectionManager = new stompit.ConnectFailover([connectOptions], reconnectOptions);

connectionManager.connect(function (error, client, reconnect) {
    if (error) {
        console.log("Terminal error, gave up reconnecting");

    client.on("error", function (error) {
        console.log("Connection lost. Reconnecting...");

    var headers = {
        "destination": "/topic/TRAIN_MVT_ALL_TOC",           // subscribe for a destination to which messages are sent
        "activemq.subscriptionName": "somename-train_mvt",   // request a durable subscription - set this to an unique string for each feed
        "ack": "client-individual"                           // the client will send ACK frames individually for each message processed
    client.subscribe(headers, function (error, message) {
        if (error) {
            console.log("Subscription failed:", error.message);
        message.readString("utf-8", function (error, body) {
            if (error) {
                console.log("Failed to read a message", error);
            if (body) {
                var data;
                try {
                    data = JSON.parse(body);
                } catch (e) {
                    console.log("Failed to parse JSON", e);
                    function(item, next) {
                        // Look for Train Activation messages (msg_type 0001)
                        if (item.header && item.header.msg_type == "0001") {
                                "activated at stanox", 
                                item.body.tp_origin_stanox ? item.body.tp_origin_stanox : item.body.sched_origin_stanox
            client.ack(message); // Send ACK frame to server

Network Rail Open Data Feeds
Data Feeds About the Feeds Account States Durable Subscriptions Example Code ( PHP / C# / Java / Ruby / Node.js) • Advanced UsesFAQ Release Notes
Train Movements Train Movements Feed Train Activation Train Cancellation Train Movement Train Reinstatement Change of Origin Change of Identity Change of Location TSPEED Field Planned Cancellations Cancellation Codes
TD TD Feed C-Class Messages S-Class Messages Train Describers TD Berths
TSR TSR Feed Route Codes
SCHEDULE SCHEDULE Feed TIPLOC Records Schedule and Location Records Association Records CIF Codes How Scheduling Works Allowances
Reference Data Reference Data Feed TOC Codes CIF Codes Delay Attribution Codes Identifying Locations (STANOX, TIPLOC, NLC and 3-Alpha Codes) STANOX Geographical Areas Train Planning data