Skip to content

Instantly share code, notes, and snippets.

@raed667
Created December 21, 2016 23:31
Show Gist options
  • Save raed667/14ea13003112a1d87b6dc372cfc3c82e to your computer and use it in GitHub Desktop.
Save raed667/14ea13003112a1d87b6dc372cfc3c82e to your computer and use it in GitHub Desktop.
Simple (& dumb) insert in ES
const mqtt = require('mqtt');
const ES = require('esta');
const INDEX = 'mqtt',
TYPE = 'eclipse';
ES.CONNECT(INDEX, function(response) {
// When DB is on => connect to mqtt
const client = mqtt.connect('mqtt://iot.eclipse.org');
client.on('connect', () => {
client.subscribe('#');
});
client.on('message', (topic, message) => {
addARecord(message.toString(), topic);
});
});
function addARecord(message, topic) {
var record = {
index: INDEX,
type: TYPE,
date: new Date().toISOString(),
message: message,
topic: topic
};
ES.CREATE(record, function(response) {
console.log(response.created);
});
}
process.on('uncaughtException', function(err) {
console.log('ERROR: ' + err);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment