gion
gion

Reputation: 13

Emulate RPC with Firebase

I am using Firebase to monitor machines across the building. So architecture is multiple front-ends and multiple machines.

At a certain moment I want to be able to trigger some actions on these machines like:

I am used to Actionscript, there are NetConnection and Client objects to whom one could invoke remote methods.

  • Is there something similar in Firebase ?
  • How would you implement such a feature easily ?

I thought of having a message box, using an Array, where a message could be a data structure like:

{ 
  'client_id': 'xxx-yyy-zzz', 
  'name': 'takeScrenshot', 
  'body': { 'creator': 'my-name' },
  'timestamp': 1406214344
}

How it might work

But to implement it correctly a lot of work must be done, does anyone know if there is an easy way to achieve this kind of functionality ?

Upvotes: 0

Views: 747

Answers (1)

Kato
Kato

Reputation: 40582

Since Firebase is a powerful backend service, scalable, and has a RESTful API in addition to SDKs (not yet for Python, unfortunately), it generally makes the most sense to just use it directly, rather than fashioning API services on top of it.

One fast and effective way to do this is to utilize a queue approach. Have each client write data into an in/ path, and have the recipient of the event listen for child_added on that path. Then perform the remote invocation, and write data back to an out/ path for the requesting client.

client

// send it
var ref = new Firebase(QUEUE_URL);
var request = ref.child('in').push( requestData );

// wait for a reply and remove after processing
ref.child('out/'+request.name()).on('value', function(snap) {
   if( snap.val() !== null ) {
      console.log(snap.val());
      request.remove();
      // stop listening
      snap.ref().off();
   }
});

remote service

var ref = new Firebase(QUEUE_URL);

// listen for queue events
ref.child('in').on('child_added', function(snap) {
   /*
     ... process queue event ...
    */
   doneProcessing(snap, resultData);
});

function doneProcessing(snap, results) {
   ref.child('out/'+snap.name()).set(results);
   snap.ref().remove();
}

Upvotes: 4

Related Questions