node.js - Limit number of data streamed to a webpage with node.jsand socket.io -


i'm working on streaming app using node.js , socket.io. moment app works need 1 thing: since it's streaming app there data push client, want display 10 recent object pushed server. how can ? should on client code or directly on server ? have no idea.

here's code of client

<script src="/socket.io/socket.io.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.js"></script> <script>      var socket = io.connect('http://192.168.1.29:5000');     socket.on('stream', function(tweet){     $('#tweetd').append(tweet+'<br>');     }); </script> <div id="tweetd"></div> </div> 

server code :

 var express = require('express')        , app = express()       , http = require('http')     , server = http.createserver(app)       ,twit = require('twit')        , io = require('socket.io').listen(server);    server.listen(8080);    // routing   app.get('/', function (req, res) {   res.sendfile(__dirname + '/index.html');   });    var watchlist = ['love', 'hate'];   var t = new twit({    consumer_key:         ''  , consumer_secret:      ''    ,  access_token:         ''   , access_token_secret: ''    });  io.sockets.on('connection', function (socket) {   console.log('connected');      t.stream('statuses/filter', { track: watchlist },function (stream) {    stream.on('tweet', function (tweet) {          io.sockets.emit('stream',tweet.text);         console.log(tweet.text);    });  }); });  

hope can me !

you should on server. saves bandwidth , time.

if have list of objects send back, slice list before send via socket.io.

tweets = tweets.slice(tweets.length-10); 

now send tweets via socket.io , you'll last 10 in list.

or if list in reverse order:

tweets = tweets.slice(0,10); 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -