I'm using Sails 0.11 in my back-end, and angularjs in the front-end.
I have a TwitterController in sails with the following code, to open a stream with the Twitter Streaming API (this uses the node module twit):
var Twit = require('twit');
var T = new Twit(sails.config.twit);
var stream = T.stream('statuses/filter', {
track: ['apple']
});
module.exports = {
open: function(req, res) {
if (!req.isSocket) {
return res.badRequest();
}
var socketId = sails.sockets.id(req.socket);
stream.start();
stream.on('tweet', function(tweet) {
sails.log.debug('Tweet received.');
sails.sockets.emit(socketId, 'tweet', tweet);
});
}
};
In my front-end (with the angular-sails module):
$sails.get('/twitter/open').then(function(resp) {
console.log(resp.status);
}, function(resp) {
alert('Houston, we got a problem!');
});
This of course reaches my back-end controller, and the streaming starts, but how do I listen to the
sails.sockets.emit(socketId, 'tweet', tweet);
issued by the server?.
I'd appreciate any help here!.
Following wZVanG's answer. This is how I did it, using the angular-sails angular module.
That logs every tweet received to the browser's console.