DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Connecting the Dots Between Firebase and Microsoft Bot Framework: Part I

Connecting the Dots Between Firebase and Microsoft Bot Framework: Part I

To use the NodeJS Azure function, you simply need to configure an environment in AWS serverless so that you can use Firebase from a NodeJS Azure function.

Ezequiel Reyno user avatar by
Ezequiel Reyno
·
Mar. 02, 17 · Tutorial
Like (4)
Save
Tweet
Share
6.17K Views

Join the DZone community and get the full member experience.

Join For Free

I've been working in an example that you can check out here. This example uses Firebase, AngularJs and Azure Functions. I used a bunch of cool tools to create rapidly powerful front-end apps. Today I'll add a couple of new tools like Microsoft Bot Framework and Azure Service Bus to enhance our app in a couple of ways.

The idea is to create a bot that receives a place name and response with the current date and time at that location. After the bot parses the input and generates the proper response, it puts into the Azure Service Bus a message that will trigger our NodeJs Azure Function 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 focus only on our Azure Function. 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 of the last article, our Azure function is composed of two files: the function.json (function metadata) and an index.js file to place our code to execute because this one is a NodeJS function. 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 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>.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. Let's dive directly into our function so I can explain to you what's going on. First, I use the method signInAnonymously under the object auth because I need a logged-in user to write in my database. Don't worry — we haven't talked about this yet, but believe me, I'm going to explain to you more about database rules and authentication with Firebase.

Then, I use the object that I receive as a parameter from my Azure Service Bus and directly store it in our database. 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 about the Azure functions, you can use this link. For more details about the NodeJs Azure functions, you can use this link. For mode details about the 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.

Firebase azure Framework Node.js

Published at DZone with permission of Ezequiel Reyno. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Testing Level Dynamics: Achieving Confidence From Testing
  • 11 Observability Tools You Should Know
  • Top 5 Data Streaming Trends for 2023
  • Microservices Testing

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: