📜 ⬆️ ⬇️

Firebase queue: steroids for firebase

About Firebase more than once wrote on Habré. The key advantage of this system is that in some cases it is possible to build a complete web application on it that works with real-time data. Having the ability to edit the rules of access to the database and the fact that these rules can be arranged on the basis of users (who are also brought here), in principle, you can do without any backend. But usually there are problems that are better solved "from the outside" than to produce bikes in the rules ( for example ).

In mid-May, the developers announced the release of firebase-queue . This is a javascript library with which you can organize work with data in the database, as with tasks. It works as follows: we define the task cell using Queue () on the server and establish a connection. Now, when a new element appears in this cell, the server will take the necessary actions, if necessary, will inform about the progress and errors, and upon completion will delete the task. As a result, we get the opportunity to cover many problems arising in development with Firebase - to reconcile the data, conduct additional validation (for example, check for spam and mate), send them to another location (for example, upload a picture to the hosting) and more.

As an example, take the above question from stackoverflow. We have an object with the n-th number of elements. After adding / deleting an item, we want to update the total count. In the database, we define two objects: elements and length . Also in the rules we specify the cells of the tasks addnode and rmnode . In them from the client we will send that object which we want to receive and delete respectively from elements .

var ref = new Firebase('https://***.firebaseio.com'); var addNode = function(text) { //  kriskowal/q   var deferred = Q.defer(); var task = ref.child('addnode').push({ new: text }, function(e) { if (e) { deferred.reject(e); } else { /*      .  progress()     _progress, resolve() -  . */ ref.child('addnode/'+task.key()).on('value', function(d) { var v = d.val(); if(v == null) { deferred.resolve(); } else { deferred.notify(v._progress); } }) }}); return deferred.promise; } var rmNode = function(k) { var deferred = Q.defer(); var task = ref.child('rmnode').push({ key: k }, function(e) { if (e) { deferred.reject(e); } else { ref.child('addnode/'+task.key()).on('value', function(d) { var v = d.val(); if(v == null) { deferred.resolve(); } else { deferred.notify(v._progress); } }) }}); return deferred.promise; } 

Firebase-queue is attached to task cells. As soon as a new task appears, we already perform the necessary manipulations on the server:
 var ref = new Firebase('https://***.firebaseio.com'); var length; ref.child('length').once('value', function(d) { length = d.val(); }); var addNodeQueue = new Queue(ref.child('addnode'), {}, function(data, progress, resolve, reject) { ref.child('elements').push(data.new, function(e) { if (e) { reject(e); } else { progress(50); length++; ref.child('length').set(length, function(e) { if (e) { reject(e.message); } else { resolve(); }}); }}); }); var rmNodeQueue = new Queue(ref.child('rmnode'), {}, function(data, progress, resolve, reject) { ref.child('elements/'+data.key).remove(function(e) { if (e) { reject(e); } else { progress(50); length--; ref.child('length').set(length, function(e) { if (e) { reject(e); } else { resolve(); }}); }}); }); 

The rules will be as follows:
')
 { "rules": { "addnode": { "$taskId": { "new": { ".validate": "newData.isString()" } } }, "rmnode": { "$taskId": { "key": { ".validate": "root.child('elements/'+newData.val()).exists()" //         } } } } } 

In order not to overload and preserve the overview nature of the article, I decided to omit the story about security rules for tasks, task specifications and options that you can specify for Queue () . This is all fine (like all of their documentation) described on the project page on github.

Source: https://habr.com/ru/post/266355/


All Articles