Comments (3)
You need to import gtfs-rb by referencing the transit_realtime property. It's a change from the old protobuf.js bindings generator, but this is done to separate namespaces.
Ex: This won't work
const gtfsRB = require('gtfs-rb')
gtfsRB.FeedMessage // won't work
This will:
const gtfsRB = require('gtfs-rb').transit_realtime
gtfsRB.FeedMessage // FeedMessage class, with encode/decode/etc.
What I do is something more like this keep my code from being cluttered with "gtfsRealtimeBindings.transit_realtime....etc" everywhere:
const gtfsRB = require('gtfs-rb').transit_realtime
// Deconstruct these classes from GTFS Realtime Bindings
const { FeedMessage, FeedHeader, FeedEntity, VehiclePosition } = gtfsRB
FeedMessage // valid FeedMessage class with all protobuf message methods
from gtfs-rb.
Ohhh, that works perfectly. Thank you!
from gtfs-rb.
Here's the full code for inspection:
"use strict";
const express = require('express');
const GtfsRealtimeBindings = require('gtfs-rb');
const request = require('request');
const cors = require('cors');
const TRIP_UPDATES_URL = "http://gtfs.halifax.ca/realtime/TripUpdate/TripUpdates.pb";
const ALERTS_URL = "http://gtfs.halifax.ca/realtime/Alert/Alerts.pb";
const VEHICLE_POSITIONS_URL = "http://gtfs.halifax.ca/realtime/Vehicle/VehiclePositions.pb";
let tripUpdates = [];
let vehiclePositions = [];
let alerts = [];
/* Get Trip Updates */
function updateAndSendTripUpdates(origResponse) {
let posReqSettings = {
method: 'GET',
url: TRIP_UPDATES_URL,
encoding: null
};
request(posReqSettings, function (error, response, body) {
if (!error && response.statusCode == 200) {
let feed = GtfsRealtimeBindings.FeedMessage.decode(body);
tripUpdates = [];
feed.entity.forEach((entity) => tripUpdates.push(entity));
origResponse.send(tripUpdates);
}
});
}
/* Start Server */
const app = express();
const port = 3000;
app.use(cors());
app.get('/', (request, response) => {
response.send("");
});
app.get('/trip_updates', (request, response) => {
updateAndSendTripUpdates(response);
});
app.listen(port, (err) => {
if (err) return console.log('ERROR', err);
console.log(`Server is listening on port ${port}`);
});
from gtfs-rb.
Related Issues (1)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gtfs-rb.