I have a Node app/script that needs to constantly be running (it's a discord bot, done with discord.js, but I think that's mostly irrelevant), and I'd like to do it on Firebase.
It has its own client.on('event', ()=>{}) events system, so I don't believe that I could use Firebase's cloud functions. There's also what seems to be a website-hosting based way to have a node.js server, but that seems triggered by HTTP requests.
Is there any other way I could do it?
There is no way to run arbitrary node.js code on Firebase. Unless your script can run within Cloud Functions "triggered execution" mode, you'll need your own app server to run it.
You can of course create a service that maps Discord.js events to Firebase events, such as writes to the Realtime Database, Cloud Firestore, even just direct HTTPS calls to a Cloud Functions endpoint. You could even bypass Firebase there and have your mapping service write to Cloud PubSub and use that to trigger Cloud Functions.
One thing that looks promising in the Discord.js documentation is their mention of web hooks, which is just another way of describing HTTP endpoints. But from my quick scan I couldn't figure out if those would allow you to call your HTTP triggered Cloud Function.
Related
I want to deploy a Node.JS script to Firebase that listens for new tweets using Twitter's streaming API, and then stores each tweet in the Firebase Database. How do I deploy this script and make sure that it runs 24/7?
As an example, consider the following (simplified) code, based on NPM Twit package:
var stream = T.stream('statuses/sample')
stream.on('tweet', function (tweet) {
// write data to Firebase Database here
})
I looked into Firebase Cloud functions, but that's not what I want, because they are triggered by an event. I also saw there is Firebase Queue, but I don't really want to queue the process: I just want it to run all the time. Is this possible currently with Firebase hosting?
An alternative would be to create a Twitter recipe with IFTTT that calls a Webbook. However, I want to store tweets from many different users, so it's not a very flexible solution.
Firebase Hosting can't run any code. Cloud Functions can run code, but not indefinitely, and only in response to defined triggers.
Consider instead using multiple IFTTT with a Cloud Function as a webhook to receive tweets that meet some search criteria, as implemented in this project. You can set up multiple IFTTT applets if you need multiple queries to be aggregated in one place. This may be your best bet if you don't want to maintain your own server.
I'm developing a chat-bot using api.ai for NLP and i'm stucked where i need to query some data from a database. And also i need to do some processing of those data. I'm not much clear how can I do this using the webhook. Any thoughts and tips would be really appreciated.
You might want to check out API.AI's getting started guide, the last part of which involves the basics of adding fulfillment to your agent which pulls from this getting started sample
We use Cloud Functions for Firebase in the sample but you can't call non-Google APIs or URLs without setting up billing. You can also use any other hosting providers (Google's AppEngine, Compute Engine, Amazon's Lambda, EC2, Heroku, Azure, etc.) for fulfilling API.AI agents, as long as you have a server that can respond to a HTTPS request, it'll work with API.AI's webhook fulfillment. To get started you can even use your local machine and tools like ngrok to tunnel the connection from a HTTPS URL to your local development machine: https://ngrok.com/. If your just starting out I'd recommend doing whatever your most comfortable with.
Am currently using Node-RED to process the data received through the webhook. The webhook receives the data in JSON and if you’ve setup your intents and entities correctly, you’ll get data that you can act on.
This can be achieved as a stand-alone nodeJS app that you host somewhere, like Heroku or OpenShift. But important point is that you need a server running a program somewhere to process the data that api.ai sends.
Am using firebase as my database and is integrated into my Node-RED setup and now I can do sophisticated queries like “how much did I walk last Wednesday”
I'm using node js with aws lambda functions for my web application. I want to use web socket in my web app. And socket.io is a very famous library for node.js.
But I'm not sure how can I use it in aws lambda. can anyone let me know how to do it, is it possible using lambda functions?
Can't use socket.io with lambdas. Lambdas have a limit on time it can be executed, so holding open connection with a client is impossible.
What you can do though is use SNS. Lambdas can publish messages to SNS topics and lambdas can be invoked with SNS.
Workaround exists for this - it's the WebSockets over MQTT in AWS IoT. This way you can execute your Lambda functions from open socket connections.
Currently, AWS API Gateway supports WebSockets.
Unfortunately, I didn't manage to connect via socket.io since it generates a custom URL with additional params: /?EIO=3&transport=polling&sid=< id>
But I've found tiny WebSocket wrapper sockette (used this tutorial), and it works fine!
We are using Firebase for an Mobile APP. We have thousands of users.
Expected to hit 100 thousand.
We are having a portal to configure data to be shown to the user.
Based on user input we need to manipulate lot of data.
Currently we are using a flag for each user for which we are having on() listener. So we are going to have thousands of listeners.
These listeners are handled from a Node JS server hosted on Heroku.
Earlier we used Parse and we had Parse cloud code to manipulate Parse Core DB on cloud code call.
But in Firebase we will eventually need to create a REST API which will do the job for us instead of having 100 thousands listeners for 100 thousand users.
But then we will need to have rewrite network code on the APP side for rest API call which is currently handled by Firebase library for which we had gone with Firebase in the fir
Every Firebase listener is a separate websocket connection, and I'm not sure how easy it will be for you to create hundreds of thousands of connections on a single node.js Heroku dyno (although see here how something similar was apparently achieved on an appropriately configured 15GB rackspace cloud server).
If you plan to run your Firebase listeners on multiple Heroku dynos, you will need a way to distribute your listeners across the different dyno instances.
I am parse.com user, and now I look for another service.
How can I write back end logic to firebase?
let say I want to validate all the values on server side, or trigger things. I thought about one solution, but I want to know the recommended way.
I think to
create nodejs server, that uses express.
create middlewares to handle the logic.
send rest request from the app, that triggers the middlewares
use the nodejs sdk of firebase to update the values according to the params of the http request.
And implement on the app firebase handler that listen to changes
their something simpler? In parse I used cloud code, I want that the logic will not be on the client side but on a server side.
Update (March 10, 2017): While the architecture I outline below is still valid and can be used to combine Firebase with any existing infrastructure, Firebase just released Cloud Functions for Firebase, which allows you to run JavaScript functions on Google's servers in response to Firebase events (such as database changes, users signing in and much more).
The common architectures of Firebase applications are pretty well-defined in this blog post Where does Firebase fit in your app?.
The architecture you propose is closest to architecture 3, where your client-side code talks both directly to Firebase and to your node.js server directly.
I also highly recommend that you consider option 2, where all interaction between clients and server runs through Firebase. A great example of this type of architecture is the Flashlight search integration. Clients write their search queries into the Firebase database. The server listens for such requests, executes the query and writes the response back to the database. The client waits for that response.
A simple outline for this server could be:
var ref = new Firebase('https://yours.firebaseio.com/searches');
ref.child('requests').on('child_added', function(requestSnapshot) {
// TODO: execute your operation for the request
var responseRef = ref.child('responses').child(requestSnapshot.key());
responseRef.set(result, function(error) {
if (!error) {
// remove the request, since we've handled it
requestSnapshot.ref().remove();
}
});
})
With this last approach the client never directly talks to your server, which removes all kind of potential problems that you have to worry about. For this reason I sometimes refer to them as "bots", instead of servers.
2017
Today Google announced Cloud Functions for Firebase
https://firebase.google.com/features/functions/
This is a great solution for the architectures and back end logic in Firebase.
Here's what I would do:
Validade all the inputs with the ".validate" rules. No server needed for that.
If you have tasks to run, use Firebase Queue, a bot to run the tasks and you are done.
If you don't do the last one, you may have two problems:
If you try use the diagram you posted it will be a little tricky to get the auth object at the server (but not impossible). Go ahead if you don't need to validate the user to allow the request.
If you use just the regular firebase app to listen to changes and respond (editing the object for instance, like Frank van Puffelen's example code), you might have scalability problems. Once your back end scales to two (or more) instances, a firebase edit will trigger the task on all of them. Each instance will notice there was a change, then run the same task once each, add/replace the response object once each and try to remove the request object once each..
Using Firebase Queue avoids both of these problems.
You can combine these two behaviors simultaneously:
Client side communicates directly with the Database
One excelent thing about the Firebase Realtime & Firestore is that you are able to listen in realtime to database changes. But is important to configure the Security Rules so the client can't modify or read data that he is not suppose to.
Client communicates with a Node.js server (or other server)
The node.js server will have adminstrative privilegies by using the Firebase Admin SDK, it can perform any change in the database regardless how the Firebase Security Rules are configured.
The Client Side should use the Firebase Authentication library to obtain the
ID Token, it will inform to the server on each request (e.g. on headers). For each received request, the node.js server verifies if the ID Token is valid by using the Firebase Admin SDK.
I created a documented GitHub project of a Node.js server that uses Firestore Database and Firebase Authentication, check the example here.