Created
November 10, 2015 17:02
-
-
Save jpatel531/418511ab6509111775a8 to your computer and use it in GitHub Desktop.
Pusher Message Queue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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