Amazon Lambda to Firebase

前端 未结 7 1467
甜味超标
甜味超标 2020-11-30 03:42

I get \'Cannot find module \'firebase\' when I try to run this in Lambda (Node.js 4.3)

var Firebase = require(\'firebase\');

Same thing hap

相关标签:
7条回答
  • 2020-11-30 03:46

    2017-03-22 edit: google just announced firebase cloud functions, which is a much better way to do this. Cloud functions work just like lambda, and can trigger from firebase events.


    Here's my solution using the REST api (so you don't need to require anything):

    var https = require('https');
    var firebaseHost = "yourapp.firebaseio.com";
    function fbGet(key){
      return new Promise((resolve, reject) => {
        var options = {
          hostname: firebaseHost,
          port: 443,
          path: key + ".json",
          method: 'GET'
        };
        var req = https.request(options, function (res) {
          res.setEncoding('utf8');
          var body = '';
          res.on('data', function(chunk) {
            body += chunk;
          });
          res.on('end', function() {
            resolve(JSON.parse(body))
          });
        });
        req.end();
        req.on('error', reject);
      });
    }
    
    function fbPut(key, value){
      return new Promise((resolve, reject) => {
        var options = {
          hostname: firebaseHost,
          port: 443,
          path: key + ".json",
          method: 'PUT'
        };
    
        var req = https.request(options, function (res) {
          console.log("request made")
          res.setEncoding('utf8');
          var body = '';
          res.on('data', function(chunk) {
            body += chunk;
          });
          res.on('end', function() {
            resolve(body)
          });
        });
        req.end(JSON.stringify(value));
        req.on('error', reject);
      });
    }
    

    You can use it like this:

    fbPut("/foo/bar", "lol").then(res => {
      console.log("wrote data")
    })
    

    And then:

    fbGet("/foo/bar").then(data => {
      console.log(data); // prints "lol"
    }).catch(e => {
      console.log("error saving to firebase: ");
      console.log(e);
    })
    
    0 讨论(0)
  • 2020-11-30 03:52

    To safely use firebase npm package (version 3.3.0) in AWS Lambda (Nodejs 4.3), Please do the following:

    'use strict';
    
    var firebase = require("firebase");
    
    exports.handler = (event, context, callback) => {
        context.callbackWaitsForEmptyEventLoop = false;  //<---Important
    
        var config = {
            apiKey: "<<apikey>>",
            authDomain: "<<app_id>>.firebaseapp.com",
            databaseURL: "https://<<app_id>>.firebaseio.com",
            storageBucket: "<<app_id>>.appspot.com",
        };
    
        if(firebase.apps.length == 0) {   // <---Important!!! In lambda, it will cause double initialization.
            firebase.initializeApp(config);
        }
    
        ...
        <Your Logic here...>
        ...
    };
    
    0 讨论(0)
  • 2020-11-30 03:56

    For me firebase-admin should do the trick. https://firebase.google.com/docs/admin/setup

    Thanks for Josiah Choi for suggesting context.callbackWaitsForEmptyEventLoop though. So lambda doesn't need to initializeFirebase everytimes. My first run was really slow.

      var firebase = require('firebase-admin');
      module.exports.Test = (event, context, callback) => {
    
      context.callbackWaitsForEmptyEventLoop = false;  //<---Important
    
      if(firebase.apps.length == 0) {   // <---Important!!! In lambda, it will cause double initialization.
        firebase.initializeApp({
          credential: firebase.credential.cert("serviceAccount.json"),
          databaseURL: <YOUR FIREBASE URL>
        });
    
      }
    
    
     firebase.database().ref('conversation').once('value').then(function(snapshot) {
        console.log (snapshot.val()) ;
       var bodyReturn = {
         input: snapshot.val()
       } ;
    
       callback(null,bodyReturn);
       context.succeed() ;
    });
    
    };
    
    0 讨论(0)
  • 2020-11-30 03:57

    Another alternative if you're using a node-based development setup is to use the node-lambda package from here. Essentially it provides wrappers to set up, test and deploy to lambda. node-lambda deploy will package up any modules you've installed (e.g. with npm i --save firebase) and make sure they're available on Lambda itself. I've found it really helpful for managing external modules.

    0 讨论(0)
  • 2020-11-30 04:00

    I solved my problem by using firebase REST api

    var https = require('https');
    
    exports.handler = function(event, context, callback) {
    
        var body = JSON.stringify({
            foo: "bar"
        })
    
       var https = require('https');
    
    var options = {
      host: 'project-XXXXX.firebaseio.com',
      port: 443,
      path: '/.json',
      method: 'POST'
    };
    
    var req = https.request(options, function(res) {
      console.log(res.statusCode);
      res.on('data', function(d) {
        process.stdout.write(d);
      });
    });
    req.end(body);
    
    req.on('error', function(e) {
      console.error(e);
    });
    
        callback(null, "some success message");
    
    }
    
    0 讨论(0)
  • 2020-11-30 04:11

    After trying a few things, this seems to work for me (v 3.10.8) :

    for(var i=0;i<5;i++)
    {
    
        var firebase = require('firebase');
        var config = {
            apiKey: "",
            authDomain: "",
            databaseURL: "",
            storageBucket: "",
            messagingSenderId: ""
          };
            if(firebase.apps)
            if(firebase.apps.length==0)
          firebase.initializeApp(config)
    
            firebase.database().ref().child("test").once('value').
              then(function(snapshot) {
                console.log(snapshot.val());
    
                                     });
    
      }
    
    0 讨论(0)
提交回复
热议问题