Node JS Examples

node.js (Stompit)

Using stompit library from https://github.com/gdaws/node-stomp

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

// Connect options with standard headers
var connectOptions = {
    "host": "publicdatafeeds.networkrail.co.uk",
    "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");
        return;
    }

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

    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);
            return;
        }
        message.readString("utf-8", function (error, body) {
            if (error) {
                console.log("Failed to read a message", error);
                return;
            }
            if (body) {
                var data;
                try {
                    data = JSON.parse(body);
                } catch (e) {
                    console.log("Failed to parse JSON", e);
                    return;
                }
                async.each(data,
                    function(item, next) {
                        // Look for Train Activation messages (msg_type 0001)
                        if (item.header && item.header.msg_type == "0001") {
                            console.log(
                                "Train", 
                                item.body.train_id, 
                                "activated at stanox", 
                                item.body.tp_origin_stanox ? item.body.tp_origin_stanox : item.body.sched_origin_stanox
                            );
                        }
                        next();
                    }
                );
            }
            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) • STOMP Client Libraries • Advanced Uses • FAQ • Release Notes
RTPPM RTPPM Feed
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
VSTP VSTP Feed
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 (BPLAN)
This article is issued from Openraildata. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.