Created
December 21, 2016 23:31
-
-
Save raed667/14ea13003112a1d87b6dc372cfc3c82e to your computer and use it in GitHub Desktop.
Simple (& dumb) insert in ES
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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