Skip to content

Instantly share code, notes, and snippets.

@richcaudle
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save richcaudle/5466f10690d39d321ad7 to your computer and use it in GitHub Desktop.
Save richcaudle/5466f10690d39d321ad7 to your computer and use it in GitHub Desktop.
// Include the library
var DataSift = require('datasift-node');
// Create a client
var ds = new DataSift('YOUR_USERNAME', 'YOUR_APIKEY');
var filter = 'interaction.content contains "music"';
// Compiles a stream from a CSDL definition:
function compileFilter(csdl) {
ds.compile({ 'csdl': csdl }, function (err, response) {
if (err)
console.log(err);
else
{
console.log("Filter hash: " + response.hash);
connect(response.hash); // Connects to DataSift
}
});
}
// Connects to DataSift and starts streaming data:
function connect(hash) {
// Set up a 'connect' event handler, which will fire when a connection is established. When connected we compile our CSDL filter and subscribe to streaming data.
ds.on('connect', function () {
console.log('Connected to DataSift');
ds.subscribe(hash);
});
// Set up 'error' handler to alert us of any errors. For more details on possible errors see [http://dev.datasift.com/docs/resources/errors](http://dev.datasift.com/docs/resources/errors).
ds.on('error', function (error) {
console.log('ERROR: ' + error);
});
// Set up 'delete' handler. Depending on the data sources you are using, you may need to delete this data to stay compliant.
ds.on('delete', function (data) {
console.log('Data deleted: ', data); // TODO: Do something useful with the data!
});
// Set up 'interaction' handler - this receives our data! This is triggered each time we receive a new interaction - a piece of data on the live stream.
ds.on('interaction', function (data) {
console.log('Recieved data: ', data);
});
// Now all handlers are set up, connect to DataSift!
ds.connect();
}
// Initiate our script by comiling the filter, which in turn will connect us to DataSift
compileFilter(filter);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment