Instantly share code, notes, and snippets.

Embed
What would you like to do?
Firebase Queue Example
{
"name": "my-queue",
"version": "1.0.0",
"description": "My Queue",
"main": "queue_worker.js",
"dependencies": {
"firebase": "2.x",
"firebase-queue": "^1.0.0"
}
}
var Firebase = require('firebase');
// Same ref as in queue_worker.js
var ref = new Firebase('https://<your-firebase>.firebaseio.com/queue');
// This doesn't need to be set every time, but helps us
// define the spec for the task in this example
ref.child('specs').set({
task_1: {
in_progress_state: 'task_1_in_progress',
timeout: 10000
}
});
// Add tasks onto the queue
var taskNumber = 0;
setInterval(function() {
ref.child('tasks').push({
taskNumber: ++taskNumber
});
}, 1000);
var Queue = require('firebase-queue'),
Firebase = require('firebase');
// The location of the Queue - can be any Firebase Location
var ref = new Firebase('https://<your-firebase>.firebaseio.com/queue');
// Creates the Queue
var options = {
specId: 'task_1',
numWorkers: 10
};
var queue = new Queue(ref, options, function(data, progress, resolve, reject) {
// Read and process task data
console.log(data);
// Do some work
var percentageComplete = 0;
var interval = setInterval(function() {
percentageComplete += 20;
if (percentageComplete >= 100) {
clearInterval(interval);
} else {
progress(percentageComplete);
}
}, 1000);
// Finish the task
setTimeout(function() {
resolve();
}, 5000);
});
@shahzadns

This comment has been minimized.

shahzadns commented May 16, 2015

very useful. Thanks.

@mafentakis

This comment has been minimized.

mafentakis commented Jun 1, 2015

nice and useful feature!
how can i deploy the queue_worker.js on the server?
thanks,
manolis

@morgs32

This comment has been minimized.

morgs32 commented Jul 2, 2015

Question! Like in this example, I want some processing of the queued task to happen after a specific timeout (say 60 seconds). Perhaps, like the sanichat example, I've got messages coming in and they have to be cleaned.
I can't tell how many workers I can use without straining the cpu.
Or how could I use some web service (azure, aws) to add a VM when the workers are falling behind.
If my goal is for every task item to get processed after exactly 60 seconds, so that none get done too late, what would you advise?
I changed the sanichat example to demonstrate the scenario: https://github.com/morgs32/sanichat-tester

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment