Developer Docs

JavaScript Web Guide

Using the Queue

Getting Started

Every Flybase app can use a queue to add jobs to be done. This queue is accessible only from our queue endpoint or via our helper libraries.

This is a simple FIFO (First In, First Out) style queue, which is meant to make it simple to use, both for adding jobs to it and getting jobs back from it.

Queuing Tasks

Here is a simple example of adding a job using the enqueue function:


var ref = new Flybase("74k8064f-cd6f-4c07-8baf-b1d241496eec", "web", "posts");
ref.enqueue({
	username: 'joe',
	message: 'Hello',
	published: new Date().getTime()
});
			

That's it, not that different than saving data to your Flybase app is it?

Dequeuing Tasks

Now we want our workers to remove jobs from the Queue.

Here is a simple example of getting a job using our dequeue function:


var ref = new Flybase("74k8064f-cd6f-4c07-8baf-b1d241496eec", "web", "posts");

function worker(){
	//	check if there are pending jobs..
	ref.getLength(function(row){
		if( row.jobs ){
			//	grab the first item from the queue and process it...
			ref.dequeue(function( data ){
				console.log( data.username + " said: " + data.message + " on " + data.published);
				worker();
			});
		}else{
			worker();
		}
	});
}
worker();
			

Ok, we actually use two functions here. The first, getLength returns how many jobs are in the queue, while our dequeue function returns the next job in the list.

You could have multiple workers running in the background, each grabbing a job and processing it, when a job is dequeued, it is removed from the list. You could just choose to not use the getLength function, we've found it handy to check if there are pending jobs before hand so you could then do something else while waiting.

Now, we're ready for the next article on deploying your apps in Flybase.