Realtime Bus Feed Error with NodeJS


#1

I am trying to get the real-time bus positions through the live-feed, but cannot get it to work.

I am using NodeJS with the gtfs-realtime-bindings library to decode the realtime bus GTFS feed. I have also tried to use protobufjs using the 2011 and 2015 versions of proto files but doesn’t work either.

The error I am consistently getting is this: “Illegal wire type of unknown field 1242 in Message”.

My full code is here:

var config = require('./buses-config.json');
var gtfsrb = require('gtfs-realtime-bindings');
var protobuf = require('protobufjs');

var request = require('request');
  var url = 'https://api.transport.nsw.gov.au/v1/gtfs/vehiclepos/buses';
  request({
    url: url,
    headers: { 'authorization':'apikey xxxxx' },
    method: 'GET'
  }, function (error, response, body) {
    console.log('Status', response.statusCode);
    console.log('Headers', JSON.stringify(response.headers));
    console.log('Reponse received', body);
	
    
    // 1: If I use this line, I get "Illegal argument: Not a valid base64 encoded string"
    var feed = gtfsrb.FeedMessage.decode(body);
	
    // 2: If I use this line, I get "Illegal wire type of unknown field 1242 in Message"
    var feed = gtfsrb.FeedMessage.decode(new Buffer(body, 'base64'));
	
	  
    // 3: If I use protobufjs, I still get "Illegal wire type for unknown field 1242 in Message .transit_realtime.FeedMessage#decode". I have tried both 2011 and 2015 versions of the proto files.
    var transit = protobuf.loadProtoFile("gtfs-realtime.proto").build("transit_realtime");
    var feed = transit.FeedMessage.decode(body);
    
    console.log(feed);
});

What am I doing wrong?


#2

Hi,

Have you tried the file from http://opendata.transport.nsw.gov.au/forum/t/illegal-value-error-when-decoding-sydneytrains-gtfs-data/149/5 at all?

Cheers,
Alistair


#3

My colleague helped me to figure it out. The problem was that the data returned was in some weird format, so when we make a request, we needed to explicitly specify the encoding to null, like below:

...
request({
    url: url,
    headers: { 'authorization':'apikey xxxxx' },
    method: 'GET',
    encoding: null
  },
...

Can someone please explain why this works exactly? What’s the encoding used as the default for the returned value?

Also, can someone from the TfNSW / OpenData team update the documentation(s) to capture this?


#4

This is something specific to your use of request package. See the docs on the encoding parameter (especially the note at the end):

encoding - Encoding to be used on setEncoding of response data. If null, the body is returned as a Buffer. Anything else (including the default value of undefined) will be passed as the encoding parameter to toString() (meaning this is effectively utf8 by default). (Note: if you expect binary data, you should set encoding: null.)


#5

Ah I see. Do you mean the code samples? I see the Node code sample doesn’t include the encoding parameter. Not sure they can include this for the binary APIs, and not include it for the text APIs. Also not sure where the best place to document this is, since it’s specific to Node. Maybe someone can update all the code samples to include the parameter? That’s probably the simplest solution.


#6

Yeah, I meant code samples. I copied the Node.js code sample directly from the website (after you click the “submit” button) and that did not work. Yes, updating the code sample would be good.


#7

Hi @jayen, @phiali, we’ll look at getting the code sample updated.

Thanks,
Alex


#8

Thanks Alejandro.