Coder Social home page Coder Social logo

Comments (3)

ciolt avatar ciolt commented on June 20, 2024 1

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.

michaelgosling avatar michaelgosling commented on June 20, 2024 1

Ohhh, that works perfectly. Thank you!

from gtfs-rb.

michaelgosling avatar michaelgosling commented on June 20, 2024

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 photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.