Created
February 19, 2017 19:50
-
-
Save andersonsantos/6f9a39ae9b22589af997447022e24bd8 to your computer and use it in GitHub Desktop.
Firebase Queue Example
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
{ | |
"name": "my-queue", | |
"version": "1.0.0", | |
"description": "My Queue", | |
"main": "queue_worker.js", | |
"dependencies": { | |
"firebase": "2.x", | |
"firebase-queue": "^1.0.0" | |
} | |
} |
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
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); |
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
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); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment