require('dotenv').config();
const skateboard = require('skateboard');
const Receiver = require('azure-iothub-receiver');
const room = new Set();
let cache; 

const setUpSocket = function() {
  skateboard({
    dir: __dirname + '/public',         
    port : process.env.PORT || 3000,                        
    transports: ['polling', 'websocket']
  }, function(stream) {
    console.log('skateboard connected');
    room.add(stream);
    if (cache !== undefined) {
      stream.write(cache);
    }
    stream.on('end', function() {
      room.delete(stream);
    });
  });
};

const receiver = new Receiver({
  connectionString: process.env.IOT_CONN_STRING,
  consumerGroup: process.env.IOT_CONSUMER_GROUP 
});

receiver.on('message', function(eventData) {
  const from = eventData.annotations['iothub-connection-device-id'];
  if (from === process.env.IOT_DEVICE_ID) {
    console.log('Message Received:', eventData.body);
    let jsonData = '';
    if (eventData.body.data) {
      eventData.body.data = new Buffer(eventData.body.data).toString();
      jsonData = JSON.stringify(eventData.body);
    } else {
      jsonData = JSON.stringify(eventData.body);
    }
    cache = jsonData;
    room.forEach(stream => stream.write(jsonData));
  }
});

receiver.on('error', function(error) {
  console.error(error);
});

setUpSocket();

@@ -954,63 +954,17 @@
 if (
-eventData.body.data) %7B%0A      eventData.body.data = new 
+Buffer.is
 Buff
@@ -984,26 +984,12 @@
 body
-.data).toString();
+)) %7B
 %0A   
@@ -998,39 +998,24 @@
  jsonData = 
-JSON.stringify(
 eventData.bo
@@ -1008,32 +1008,42 @@
 = eventData.body
+.toString(
 );%0A    %7D else %7B%0A

fix message parsing logic