Home > Long-Polling-Buffer

Long-Polling-Buffer

Long-Polling-Buffer is a project mainly written in JavaScript, it's free.

A Library for Node.js that makes comet AJAX long polling simple.

Long Polling Buffer

(C) Rob Righter (@robrighter) 2009, Licensed under the MIT-LICENSE

A Library for Node.js to simplify AJAX long polling

API

LongPollingBuffer(buffersize)

Constructor. Initializes the buffer to be of size buffersize

push(value)

Pushes data onto the queue which in turn notifies all the listeners

addListenerForUpdateSince(since, callback)

Adds a listener for data updates. The callback is triggered when data is available after the since.

Example usage:

var sys = require('sys');
var lpb = require("./lib/longpollingbuffer");

var buffer = new lpb.LongPollingBuffer(8); //create a buffer with a size of 8

buffer.push("I'm");
buffer.push('affraid');
buffer.push('the Death Star');
buffer.push('will be');
buffer.push('fully');
buffer.push('operational');
buffer.push('when');
buffer.push('your');
buffer.push('friends');
buffer.push('arrive');

//Since forever (or to the size of the buffer)
buffer.addListenerForUpdateSince(-1, function(data){
 sys.puts('

Since forever (or to the size of the buffer): 
' + data.map(JSON.stringify).join(',
') );
});

//Since offset 6
buffer.addListenerForUpdateSince(6, function(data){
 sys.puts('

Since offset 6: 
' + data.map(JSON.stringify).join(',
') );
});

You can also see an example of LongPollingBuffer used in a webapp here.