Subscribe to Events
To subscribe to events from a device, connect to the MQTT API and subscribe it to the events topic for that device.
To subscribe to all events the MQTT topic is:
devices/{device.id}/events/+
To subscribe to a specific event the MQTT topic is:
devices/{device.id}/events/{event.name}
Replace {device.id}
with your device ID (should begin with dev_
) and {event.name}
with the name of the event you wish to subscribe to.
Example Code
Subscribe to all events using the Wia SDK
x
// Create an instance of Wia
// Replace 'd_sk_abcdef' with your device secret key
var wia = require('wia')('d_sk_abcdef');
// Subscribe to all events for a device
// Replace 'dev_abc123' with your device ID
wia.events.subscribe({
device: 'dev_abc123'
}, function(event) {
console.log(event);
});
// Connect to the MQTT API
wia.stream.connect();
Subscribe to only temperature
events using the Wia SDK
// Create an instance of Wia
// Replace 'd_sk_abcdef' with your device secret key
var wia = require('wia')('d_sk_abcdef');
// Subscribe to just 'temperature' events for a device
// Replace 'dev_abc123' with your device ID
wia.events.subscribe({
device: 'dev_abc123',
name: 'temperature'
}, function(event) {
console.log(event);
});
// Connect to the MQTT API
wia.stream.connect();