I got the twits, I'm printing all of them ok in console, but I would like send the data to the view and I don't know how can I do it using socketio.
any idea?
var TwitterController = {
'index': function(req,res) {
var twitter = require('ntwitter');
var twit = new twitter({
consumer_key: '...',
consumer_secret: '...',
access_token_key: '...',
access_token_secret: '...'
});
twit.stream('statuses/filter', { track: ['dublin', 'spain']} , function(stream) {
stream.on('data', function (data) {
console.log(data.user.screen_name + ': ' + data.text);
req.socket.emit('tweet', {
user: data.user.screen_name,
text: data.text
});
});
});
res.view();
},
};
module.exports = TwitterController;
and in the view I'm trying to print all the twits
<ul class="tw"></ul>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost:1337');
jQuery(function($){
var twList = $('ul.tw');
});
socket.on('tweet', function (data) {
twList.prepend('<li>' + data.user + '</li>');
});
</script>
If you give Sails v0.9 a try, you'll have an easier time w/ this out of the box since the bundled example handles boilerplate connection logic for you.
The key is, instead of using
res.view()
, serve tweets withres.json(tweets)
.In this example, you're using streams, so you can take advantage of
res.pipe(stream)
.Then you can use sails.io.js on the front-end to send a socket.io request to
/twitter
, and use the results for fun and profit.