I've search internet for days to find an optimized solution for having a live collection in backbone.js. think of a virtual room (e.g chat room) and I wanted to show who is in that room. right now I fetch collection each 2 sec and add new user (in my case player) to the list view, and it's working but I'm afraid of traffic and all of those useless ajax request that return nothing.
here is my backbone scripts:
App.players = new App.Collections.Players;
waitingUserList = new App.Views.WaitingPlayers({ collection: App.players });
setInterval(function() {
App.players.fetch({
success: function(collection) {
collection.each(function(player) {
if (player.get('added') == 0) {
player.set('added', 1);
player.save();
}
});
},
});
}, 2000);
$('div.waiting-list').append(waitingUserList.el);
and my php code (I'm using laravel 3):
public function get_index($gid)
{
$players = Game::find($gid)->players()->get();
if (!empty($players)) {
return Response::eloquent($players);
} else {
return false;
}
}
Is there any way that, when collection.fetch()
called, it wait until some new record added to database, and then server respond to ajax request.
P.S. I don't want to use html5 websockets
P.S.S. this is my first backbone experience, so forgive me if I've been mistaken somewhere or said something foolish :D
thanks in advance