Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use my own NodeJS server in between the client and Firebase and still use methods like "on()"?

I am creating a web application using Firebase. The client of the application communicates to my own NodeJS server which in turn communicates with Firebase app, if necessary.

Firebase offers some cool features, for example the on() function which automatically updates whenever data in Firebase is modified.

Is there any way for me to use this in my NodeJS app so that my server passes on the update to the client, without the client having to connect to Firebase directly?

I've tried including an on function like this, but I get an error when data is updated and the server crashes. Apparently, Node can't just send another response if there wasn't an explicit request. Any tips?

app.get('/project/save', function(req, res) {
    var result = firebase_admin.database().ref().on('value', function(d){
        console.log(d.val());
        return res.status(200).json(d.val());
    });
});
like image 708
CodyBugstein Avatar asked Jan 17 '26 05:01

CodyBugstein


1 Answers

You can definitely put your own node.js server between your users and the Firebase Database. But by doing so, you'll be taking on the role of app server. And that means that you'll also have to provide your own implementation/proxy for Firebase features, such as the on() methods.

The approach in the linked answer is way easier and gets you most of the same benefits. It is also documented in this documentation:

Firebase sits between app and back-end

So here both your users and your back-end code (in this case running on App Engine Flexible Environment, but it could run on any trusted environment) are essentially clients for the Firebase Database.

We recently gave a talk at the Firebase Dev Summit, where we covered this approach too: Develop mobile apps without infrastructure.

like image 162
Frank van Puffelen Avatar answered Jan 19 '26 17:01

Frank van Puffelen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!