Firebase + Azure Functions + Azure Service Bus + Microsoft Bot Framework – Connecting the dots.

What’s up guys, Eze is here. I’ve been working in an example that you can check it here, this example uses Firebase, AngularJs and Azure Functions. I used a bunch of cool tools to create rapidly powerful front-end app, in case you missed it you can check here. Today I’ll add couple of new tools like Microsof Bot Framework and Azure Service Bus to enhance our app in a decouple way. The idea is to create a bot that receives a place name and response with the current date time at that location. After the bot parse the input and generate the proper response puts into the Azure Service Bus a message that will trigger our NodeJs Azure Funtion to store the data in Firebase, pretty cool, isn’t it? You can check the working version here.

NodeJs Azure Function

Today we are going to put the focus only in our Azure Function so I’ll talk about how to create a NodeJs Azure function and how to configure it in order to be able to use Firebase from a NodeJs Azure function.

As we did last time, in case you missed it you can check here our Azure function is composed for two files the function.json (function metadata) and because this one is a NodeJs function we need to have an index.js file to place our code to execute. All JavaScript functions must export a single function via module.exports for the runtime to find the function and run it. This function must always include a context object. In the example below you can see how I configure my function.

module.exports = function(context, mySbMsg) {
   if(!app) {
      context.log('Initializing Firebase App');
      app = firebase.initializeApp(config);
   }
   var defaultAuth = app.auth();
   if(!_user){
      defaultAuth.signInAnonymously()
                 .catch(function(error) {
                    context.log('Anonymous login failed: ' + error.message);
                    context.done();
                 });
   }

   defaultAuth.onAuthStateChanged(function(user) {
   if (user) {
      _user = user;
      context.log('User is signed in. user uid: ' + _user.uid);
      var defaultDatabase = app.database().ref().child('requests').child(_user.uid).child('userRequests');
      mySbMsg['userName'] = 'Anonymous';
      defaultDatabase.push(mySbMsg)
                     .then(function() {
                          context.log('JavaScript ServiceBus queue trigger function processed message', mySbMsg);
                          context.done();
                     })
                     .catch(function(error) {
                          context.log('Synchronization failed');
                          context.done();
                      });
    } else {
       context.log('User is signed out.');
       context.done();
    }});
};

Configuring the environment

Before going further with the explanation of the function I want to explain first how to configure the Azure function to use Firebase node package. Let’s remember that this function is going to run in Azure serverless. That only means that we don’t have full control over our server but we still can run npm install  in the function app’s SCM (Kudu) command line interface in order to add packages to our app:

  1. Navigate to: https://<function_app_name&gt;.scm.azurewebsites.net.
  2. Click Debug Console > CMD.
  3. Navigate to D:\home\site\wwwroot\<function_name>.
  4. Run npm install firebase --save.

kudu

After doing this we will be able to import our Firebase library like this:

var firebase = require('firebase');

After that we can use our firebase object as we did before so let’s dive directly into our function so I can explain you what’s going on. First I use the method signInAnonymously under the object auth and this is because I need a logged in user to write in my database, don’t worry we didn’t talk about it yet but believe me I’m going to explain you more about database rules and authentication with Firebase.

Then I use the object that I receive as parameter from my Azure Service Bus and directly store it in our database. During this week I’ll be posting about the other parts of this system so you can know more about Microsoft Bot Framework and Azure Service Bus

For more details of Azure Functions you can use this link.

For more details of NodeJs Azure Functions you can use this link.

For mode details of Firebase npm package us this link.

If you found this post useful please don’t forget to press the like button and share it. If you are in doubt don’t hesitate to ask a question and as always thank you for reading.

Advertisements

One thought on “Firebase + Azure Functions + Azure Service Bus + Microsoft Bot Framework – Connecting the dots.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s