JavaScript GTFS-realtime Language Bindings¶
Provides JavaScript classes and their associated types generated from the GTFS-realtime Protocol Buffer specification. These classes will allow you to parse a binary Protocol Buffer GTFS-realtime data feed into JavaScript objects.
These bindings are designed to be used in the Node.js environment, but with some effort, they can probably be used in other JavaScript environments as well.
We use the ProtoBuf.js library for JavaScript Protocol Buffer support.
Add the Dependency¶
To use the gtfs-realtime-bindings
classes in your own project, you need to
first install our Node.js npm package:
npm install gtfs-realtime-bindings
Example Code¶
The following Node.js code snippet demonstrates downloading a GTFS-realtime data feed from a particular URL, parsing it as a FeedMessage (the root type of the GTFS-realtime schema), and iterating over the results.
In order to make this example work, you must first install node-fetch
with NPM.
Note: this exemple is using ES modules (import
/export
syntax) and is not compatible
with CommonJS (require
syntax). You can use CommonJS by converting import
to require
and installing node-fetch@2
. Learn more about ES modules here.
import GtfsRealtimeBindings from "gtfs-realtime-bindings";
import fetch from "node-fetch";
(async () => {
try {
const response = await fetch("<GTFS-realtime source URL>", {
headers: {
"x-api-key": "<redacted>",
// replace with your GTFS-realtime source's auth token
// e.g. x-api-key is the header value used for NY's MTA GTFS APIs
},
});
if (!response.ok) {
const error = new Error(`${response.url}: ${response.status} ${response.statusText}`);
error.response = response;
throw error;
process.exit(1);
}
const buffer = await response.arrayBuffer();
const feed = GtfsRealtimeBindings.transit_realtime.FeedMessage.decode(
new Uint8Array(buffer)
);
feed.entity.forEach((entity) => {
if (entity.tripUpdate) {
console.log(entity.tripUpdate);
}
});
}
catch (error) {
console.log(error);
process.exit(1);
}
})();
For more details on the naming conventions for the JavaScript classes generated from the gtfs-realtime.proto, check out the ProtoBuf.js project which we use to handle our Protocol Buffer serialization.