Node JS Examples: Difference between revisions
Another example |
EvelynSnow (talk | contribs) datafeeds.networkrail.co.uk→publicdatafeeds.networkrail.co.uk |
||
(2 intermediate revisions by one other user not shown) | |||
Line 7: | Line 7: | ||
var destination = '/topic/TRAIN_MVT_ALL_TOC', | var destination = '/topic/TRAIN_MVT_ALL_TOC', | ||
client = new StompClient(' | client = new StompClient('publicdatafeeds.networkrail.co.uk', 61618, 'emailaddress', 'your_password', '1.0'); | ||
client.connect(function(sessionId) { | client.connect(function(sessionId) { | ||
Line 27: | Line 27: | ||
// Connect options with standard headers | // Connect options with standard headers | ||
var connectOptions = { | var connectOptions = { | ||
"host": " | "host": "publicdatafeeds.networkrail.co.uk", | ||
"port": 61618, | "port": 61618, | ||
"connectHeaders": { | "connectHeaders": { | ||
Line 64: | Line 64: | ||
"destination": "/topic/TRAIN_MVT_ALL_TOC", // subscribe for a destination to which messages are sent | "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 | "activemq.subscriptionName": "somename-train_mvt", // request a durable subscription - set this to an unique string for each feed | ||
"ack": "client" | "ack": "client-individual" // the client will send ACK frames individually for each message processed | ||
}; | }; | ||
Line 95: | Line 95: | ||
item.body.tp_origin_stanox ? item.body.tp_origin_stanox : item.body.sched_origin_stanox | item.body.tp_origin_stanox ? item.body.tp_origin_stanox : item.body.sched_origin_stanox | ||
); | ); | ||
} | } | ||
next(); | |||
} | } | ||
); | ); |
Latest revision as of 11:55, 25 February 2023
node.js
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('publicdatafeeds.networkrail.co.uk', 61618, 'emailaddress', 'your_password', '1.0'); client.connect(function(sessionId) { console.log('Trying to connect...'); client.subscribe(destination, function(body, headers) { console.log(prettyjson.render(JSON.parse(body))); }); });
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) • 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) |