Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't push data to Firebase from within an Alexa Skill hosted on AWS Lambda

I have a database in Firebase to which I'm trying to write some data from within my Alexa Skill. The Node.js code for that skill sits inside an AWS Lambda function and when that code is run I want to push some data to Firebase.

I've tested the code that connects to and pushes to Firebase outside of Lambda and it works exactly as expected. Below is that code:

var firebase = require('firebase');

firebase.initializeApp({
    databaseURL: 'https://myapp.firebaseio.com',
    serviceAccount: './myapp.json',
});

var cart = firebase.database().ref('/cart'); 
console.log(cart);
    cart.push( {
    item: 'apples', quantity: '1', amount: '0'
},  function(error) {
    if (error) {
         console.log("Data could not be saved." + error);
    } else {
         console.log("Data saved successfully.");
    }
});

This same code doesn't push anything to the database instance when executed from within the Lambda function. I read online that Lambda timeout limit could be a reason for this, so I increased the timeout limit to a minute, and it still doesn't run as expected. I've also tried using the Firebase REST API instead of their Node.js SDK and that didn't work either. What is the correct way to push data to Firebase from within AWS Lambda?

like image 575
irams Avatar asked Oct 20 '16 23:10

irams


People also ask

Does firebase work with AWS?

With Firebase consisting of proprietary services, APIs, and an SDK, a migration to AWS requires application refactoring – introducing a new architecture using AWS services, and rewriting parts of the codebase to use them accordingly.

What is AWS lambda in Alexa?

PDFRSS. You can use Lambda functions to build services that give new skills to Alexa, the Voice assistant on Amazon Echo. The Alexa Skills Kit provides the APIs, tools, and documentation to create these new skills, powered by your own services running as Lambda functions.


2 Answers

I think I know why this happens, I had a similar issue and this is what I've done.

If you want to write some date into your database you need to make sure that you don't call this.emit(*****) until you are done. As soon as you return the response to the user the Thread gets closed and your information doesn't get saved.

The easiest way to solve this problem is to return the response to the user once you get confirmation that the information has been saved.

In case of Firebase something like this:

function writeUserData(userId) {

// Get a key for a new Post.
var userKey = db.ref('users/').push().key;

var reference = db.ref('users/' + userKey).set({
    user: userId
});

reference.then(() => {
        alexa.emit(':ask', "Works");
    },
    (err) => {
        alexa.emit(':ask', "Does not work");
    });

}

I couldn't get anything saved until I started doing it like this.

Hope it helps.

like image 136
Lancelot Avatar answered Sep 18 '22 13:09

Lancelot


I've run into this too and the only way I've figured out how to get it to work is to delay the lambda callback function in the handler. Try this and let me know if it works.

var firebase = require('firebase');

firebase.initializeApp({
    databaseURL: 'https://myapp.firebaseio.com',
    serviceAccount: './myapp.json',
});

exports.handler = (event, context, callback) => {
    var cart = firebase.database().ref('/cart'); 
    console.log(cart);
        cart.push( {
        item: 'apples', quantity: '1', amount: '0'
        setTimeout(()=>{
            callback(null, 'success');
        },2000);
    },  function(error) {
        if (error) {
             console.log("Data could not be saved." + error);
             setTimeout(()=>{
                 callback(error);
             },2000);
        } else {
             console.log("Data saved successfully.");
             setTimeout(()=>{
                 callback(null, 'success');
             },2000);
        }
    });
}
like image 32
mattc19 Avatar answered Sep 17 '22 13:09

mattc19