@digitransit/

Vehicle_Positions_MQTT_raw_JSON

Nodejs

Print raw JSON data received from the HFP API

fork
loading
Files
  • index.js
  • package-lock.json
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
const mqtt = require('mqtt');

const topic = '/hfp/v1/journey/#';

const client  = mqtt.connect('mqtts://mqtt.hsl.fi:8883');

client.on('connect', () => {
  client.subscribe(topic);
  console.log('Connected');
});
 
let count = 0;

client.on('message', (topic, message) => {
    if (count++ < 3) {
      console.log("---");
      console.log(topic);
      console.log(JSON.stringify(JSON.parse(message), null, 4));
    } else {
      //Close connection after receiving 3 messages
      client.end(true);
    }
});
node v10.16.0