Is there a way to split and loop over each client while maintaining control of timing? I need to emit all tweets but with a set time in between each one. Here's the code so far that throws an exception:
var socketList = new Array();
io.on('connection',function(socket){
socketList.push(socket);
});
setInterval(function() {
socketList.forEach('tweet', function(tweet) {
io.sockets.emit('info', {tweet: tweet});
});
}, 2000);
This code works but streams the tweets too quickly:
io.on('connection', function (socket) {
stream.on('tweet', function(tweet) {
socket.emit('info', {tweet: tweet});
});
});
EDIT This code doesn't give any errors but doesn't stream any tweets:
var io = require('socket.io').listen(server);
var stream = T.stream('statuses/filter', { track: 'lol' })
var socketList = new Array();
io.on('connection',function(socket){
socketList.push(socket);
});
function EmitTweets() {
stream.socketList.forEach('tweet', function(tweet) {
io.sockets.emit('info', {tweet: tweet});
});
setTimeout(EmitTweets, 1000);
}
Not sure where else to call "stream"
setInterval is synchronous. Try this:
function emitTweets() {
socketList.forEach('tweet', function(tweet) {
io.sockets.emit('info', { tweet: tweet });
});
// this is the part that makes it repeat
setTimeout(emitTweetes, 2000);
}