Skip to content

Instantly share code, notes, and snippets.

@tonespy
Forked from jpatel531/queue.js
Created June 1, 2016 08:39
Show Gist options
  • Save tonespy/88d0444f1df6b1bd6e3188a92482fbe3 to your computer and use it in GitHub Desktop.
Save tonespy/88d0444f1df6b1bd6e3188a92482fbe3 to your computer and use it in GitHub Desktop.
Pusher Message Queue
function MessageQueue(){
this.pusher = new Pusher('key');
this.items = [];
this.timeWindow = 100; // 100 ms
}
// send messages every 100ms
MessageQueue.prototype.cycle = function(){
var self = this;
this.interval = setInterval(function(){
if (self.items.length > 0 ) {
var item = self.items.shift();
self.pusher.channel(item.channel).trigger(item.event, item.data);
}
}, this.timeWindow);
};
MessageQueue.prototype.add = function(channel, event, data) {
this.items.push({
channel: channel,
event: event,
data: data
});
};
// cleanup
MessageQueue.prototype.dispose = function(){
this.clearInterval(this.interval);
this.items = [];
};
var queue = new MessageQueue();
queue.cycle(); // start queue
// add to the queue
queue.add('test_channel', 'my_event', {hello: 'world'});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment