# Realtime Bus Feed Error with NodeJS

**URL:** <https://opendataforum.transport.nsw.gov.au/t/realtime-bus-feed-error-with-nodejs/388>\
**Category:** Uncategorized\
**Created:** [October 26, 2016, 3:48am UTC](https://opendataforum.transport.nsw.gov.au/t/realtime-bus-feed-error-with-nodejs/388 "2016-10-26T03:48:31Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![hkotfnsw](https://avatars.discourse-cdn.com/v4/letter/h/77aa72/32.png) [@hkotfnsw](https://opendataforum.transport.nsw.gov.au/u/hkotfnsw)\
**Post date:** [October 26, 2016, 3:48am UTC](https://opendataforum.transport.nsw.gov.au/t/realtime-bus-feed-error-with-nodejs/388/1 "2016-10-26T03:48:31Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![phiali](https://sea1.discourse-cdn.com/flex019/user_avatar/opendataforum.transport.nsw.gov.au/phiali/32/221_2.png) [@phiali](https://opendataforum.transport.nsw.gov.au/u/phiali)\
**Post date:** [October 26, 2016, 5:21am UTC](https://opendataforum.transport.nsw.gov.au/t/realtime-bus-feed-error-with-nodejs/388/2 "2016-10-26T05:21:06Z")

</div>

Hi,

Have you tried the file from [Illegal value Error when decoding SydneyTrains GTFS data - #5 by yvonne.lee - Real-time Public Transport - Open Data - Transport for NSW forum](http://opendata.transport.nsw.gov.au/forum/t/illegal-value-error-when-decoding-sydneytrains-gtfs-data/149/5) at all?

Cheers,  
Alistair

---

<div class="post-metadata">

**Author:** ![hkotfnsw](https://avatars.discourse-cdn.com/v4/letter/h/77aa72/32.png) [@hkotfnsw](https://opendataforum.transport.nsw.gov.au/u/hkotfnsw)\
**Post date:** [October 26, 2016, 5:42am UTC](https://opendataforum.transport.nsw.gov.au/t/realtime-bus-feed-error-with-nodejs/388/3 "2016-10-26T05:42:54Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![jayen](https://sea1.discourse-cdn.com/flex019/user_avatar/opendataforum.transport.nsw.gov.au/jayen/32/208_2.png) [@jayen](https://opendataforum.transport.nsw.gov.au/u/jayen)\
**Post date:** [November 8, 2016, 4:16am UTC](https://opendataforum.transport.nsw.gov.au/t/realtime-bus-feed-error-with-nodejs/388/4 "2016-11-08T04:16:06Z")

</div>

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.)

---

<div class="post-metadata">

**Author:** ![jayen](https://sea1.discourse-cdn.com/flex019/user_avatar/opendataforum.transport.nsw.gov.au/jayen/32/208_2.png) [@jayen](https://opendataforum.transport.nsw.gov.au/u/jayen)\
**Post date:** [November 9, 2016, 2:37am UTC](https://opendataforum.transport.nsw.gov.au/t/realtime-bus-feed-error-with-nodejs/388/5 "2016-11-09T02:37:51Z")

</div>

> [@hkotfnsw](#):
>
> Also, can someone from the TfNSW / OpenData team update the documentation(s) to capture this?

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.

---

<div class="post-metadata">

**Author:** ![hkotfnsw](https://avatars.discourse-cdn.com/v4/letter/h/77aa72/32.png) [@hkotfnsw](https://opendataforum.transport.nsw.gov.au/u/hkotfnsw)\
**Post date:** [November 9, 2016, 5:58am UTC](https://opendataforum.transport.nsw.gov.au/t/realtime-bus-feed-error-with-nodejs/388/6 "2016-11-09T05:58:42Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![alejandro.felman](https://avatars.discourse-cdn.com/v4/letter/a/bbe5ce/32.png) [@alejandro.felman](https://opendataforum.transport.nsw.gov.au/u/alejandro.felman)\
**Post date:** [November 10, 2016, 12:56am UTC](https://opendataforum.transport.nsw.gov.au/t/realtime-bus-feed-error-with-nodejs/388/7 "2016-11-10T00:56:02Z")

</div>

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

Thanks,  
Alex

---

<div class="post-metadata">

**Author:** ![hkotfnsw](https://avatars.discourse-cdn.com/v4/letter/h/77aa72/32.png) [@hkotfnsw](https://opendataforum.transport.nsw.gov.au/u/hkotfnsw)\
**Post date:** [November 10, 2016, 5:05am UTC](https://opendataforum.transport.nsw.gov.au/t/realtime-bus-feed-error-with-nodejs/388/8 "2016-11-10T05:05:04Z")

</div>

Thanks Alejandro.

---

<div class="post-metadata">

**Author:** ![jasmeetsehgal](https://sea1.discourse-cdn.com/flex019/user_avatar/opendataforum.transport.nsw.gov.au/jasmeetsehgal/32/444_2.png) [@jasmeetsehgal](https://opendataforum.transport.nsw.gov.au/u/jasmeetsehgal)\
**Post date:** [July 2, 2019, 11:46pm UTC](https://opendataforum.transport.nsw.gov.au/t/realtime-bus-feed-error-with-nodejs/388/9 "2019-07-02T23:46:29Z")

</div>

I still get “TypeError: Cannot read property ‘decode’ of undefined” with following code  
var requestSettings = {  
method: ‘GET’,  
url: ‘[https://api.transport.nsw.gov.au/v1/gtfs/vehiclepos/buses?debug=true](https://api.transport.nsw.gov.au/v1/gtfs/vehiclepos/buses?debug=true)’,  
//encoding: null,  
proxy: ‘[http://127.0.0.1:3128](http://127.0.0.1:3128)’,  
rejectUnauthorized: false,  
headers: {  
‘Accept’: ‘text/plain’,  
‘Authorization’: ‘apikey oEE3PvdBdfY22VrFbsq9iuaWE2FDVbmnuypp’  
}  
};  
request(requestSettings, function (error, response, body) {  
if (!error && response.statusCode == 200) {  
console.log("Success :: ", body);  
var feed = GtfsRealtimeBindings.FeedMessage.decode(body);  
feed.entity.forEach(function(entity) {  
if (entity.trip\_update) {  
console.log(entity.trip\_update);  
}  
});  
}  
});

---

<div class="post-metadata">

**Author:** ![mattdavidson](https://sea1.discourse-cdn.com/flex019/user_avatar/opendataforum.transport.nsw.gov.au/mattdavidson/32/406_2.png) [@mattdavidson](https://opendataforum.transport.nsw.gov.au/u/mattdavidson)\
**Post date:** [July 4, 2019, 6:23am UTC](https://opendataforum.transport.nsw.gov.au/t/realtime-bus-feed-error-with-nodejs/388/10 "2019-07-04T06:23:39Z")

</div>

@jasmeetsehgal you shouldn’t use the GtfsRealtimeBindings package because TfNSW uses an extension to the GTFS proto.

I have made you a small repo that shows how I have used the api in node.

> **[GitHub - mjdavidson/tfnsw-realtime: code taken from...](https://github.com/mjdavidson/tfnsw-realtime)**
>
> code taken from https://github.com/dymajo/waka-server - GitHub - mjdavidson/tfnsw-realtime: code taken from https://github.com/dymajo/waka-server

hope that helps

---

<div class="post-metadata">

**Author:** ![mattdavidson](https://sea1.discourse-cdn.com/flex019/user_avatar/opendataforum.transport.nsw.gov.au/mattdavidson/32/406_2.png) [@mattdavidson](https://opendataforum.transport.nsw.gov.au/u/mattdavidson)\
**Post date:** [July 4, 2019, 6:49am UTC](https://opendataforum.transport.nsw.gov.au/t/realtime-bus-feed-error-with-nodejs/388/11 "2019-07-04T06:49:43Z")

</div>

also written it in typescript on the ‘ts’ branch

---

<div class="post-metadata">

**Author:** ![jayen](https://sea1.discourse-cdn.com/flex019/user_avatar/opendataforum.transport.nsw.gov.au/jayen/32/208_2.png) [@jayen](https://opendataforum.transport.nsw.gov.au/u/jayen)\
**Post date:** [July 19, 2019, 2:41am UTC](https://opendataforum.transport.nsw.gov.au/t/realtime-bus-feed-error-with-nodejs/388/12 "2019-07-19T02:41:00Z")

</div>

While Matt is right, you should still be able to decode the message, just not every field will have a name.

From the error it looks like `GtfsRealtimeBindings.FeedMessage` didn’t load properly. Nothing TfNSW related and you’d probably have the same issue with the [examples](https://developers.google.com/transit/gtfs-realtime/examples/).
