Skip to content

Instantly share code, notes, and snippets.

@idlem1nd
Last active January 18, 2017 11:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save idlem1nd/8667222f74652f78569e to your computer and use it in GitHub Desktop.
Save idlem1nd/8667222f74652f78569e to your computer and use it in GitHub Desktop.
$.connection.hub.logging = true;
$.connection.hub.url = "https://push-api.tfl.gov.uk/signalr/hubs/signalr";
var hub = $.connection.predictionsRoomHub;
hub.client.showPredictions = updateBoard;
$.connection.hub.start()
.done(function() {
console.log("tfl.predictions: connection started");
var lineRooms = [{ "LineId": "northern", "NaptanId": "940GZZLUODS" }];
hub.server.addLineRooms(lineRooms)
.done(function () {
console.log("tfl.predictions: Invocation of addLineRooms succeeded");
return;
})
.fail(function (error) {
console.log("tfl.predictions: Invocation of addLineRooms failed. Error: " + error);
return;
});
});
function updateBoard(data) {
$("#board").empty();
data.sort(sortByTts);
$.each(data, function( index, prediction ) {
var mins = Math.floor(prediction.TimeToStation/60);
var due = mins === 0 ? "Due" : mins + "m";
$("#board").append("<tr><td>" + prediction.Towards + "</td><td>" + due + "</td><td>" + prediction.CurrentLocation + "</td></tr>");
});
console.log(data);
return true;
};
function sortByTts(a, b) {
return ((a.TimeToStation < b.TimeToStation) ? -1 : ((a.TimeToStation > b.TimeToStation) ? 1 : 0));
};
@idlem1nd
Copy link
Author

idlem1nd commented Dec 9, 2015

Instructions

To use the above, make sure you also include the "hubs" javascript, which provides the connection object used in the example. There is a demo at jsFiddle.

https://push-api.tfl.gov.uk/signalr/hubs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment