0

I have an Azure IOT solution where data from 2 devices go to the same IOT hub. From my computer I need to read the messages only from one of the devices. I implemented the ReadDeviceToCloudMessages.js in https://learn.microsoft.com/en-us/azure/iot-hub/iot-hub-node-node-getstarted

var client = EventHubClient.fromConnectionString(connectionString);
client.open()
.then(client.getPartitionIds.bind(client))
.then(function (partitionIds) {
    return partitionIds.map(function (partitionId) {
        return client.createReceiver('todevice', partitionId, { 'startAfterTime' : Date.now()}).then(function(receiver) {
            console.log('Created partition receiver: ' + partitionId)
            receiver.on('errorReceived', printError);
            receiver.on('message', printMessage);
        });
    });
})
.catch(printError);

But I am getting all the messages in the IOThub. How do I get messages only from one device.

Aparna
  • 835
  • 2
  • 23
  • 47

1 Answers1

0

You can route the expected device message to build-in endpoint: events. Then you can only receive the selected device message from your above code.

Create the route:

enter image description here

Turn "Device messages which do not match any rules will be written to the 'Events (messages/events)' endpoint." to off and make sure the route is enabled.

enter image description here

Rita Han
  • 9,574
  • 1
  • 11
  • 24
  • Thank you for your answer...so will the data from device2 not go into the iothub because there is no route for it? also is there a way to specify/filter in client.createReceiver to only receive data from device1 or device2 or if only some string is present in the data? – Aparna Feb 21 '18 at 15:07
  • Yes, not route device2 message to the "Events(messages/events)" endpoint. There is a ReceiverOptions parameter you can have a check. Or if the two devices messages routed into two different partitions you can specify the partitionId parameter. – Rita Han Feb 22 '18 at 06:40