I have some pub / sub setup on Google Cloud and I have some Cloud Functions running on Firebase.
I'd like to set a trigger on Firebase, so when a user account is created, I publish a message to a topic on Google Cloud.
Is this possible? Am I missing something obvious?
I can trigger a cloud function on account creation and I'm happy at this point showing a console log.
I was considering in my Firebase trigger add something like #google-cloud/pubsub so I can literally setup the message in the body of my firebase trigger, but that feels a little wrong.
Quite clunky and not the way it should be done?
In short, I guess what I'm trying to do is firebase publish to topic on trigger?
I don't think there's anything wrong with what you're suggesting as a solution. You can use an authentication trigger to find out when a new user account is created, then use the node Cloud Pubsub SDK to turn around and publish a message to your topic. There's really no more straightforward way to accomplish this that I can think of.
It's pretty common to mix Cloud APIs into your Cloud Functions, regardless if they're based on Cloud or Firebase events.
Related
I'm using simply console.log('some flag', someObject) on Google Cloud and functions.logger.log('some flag', someObject) on firebase functions.
The problem is that both Firebase's admin panel logs page and Google Cloud's logs page makes this hard to read. Logs are getting split into lines. If my log had line breaks, then each line will be displayed as a separate log. Moreover, Google Clodu clearly has some kind of racing conditions when it comes to recording logs, as my logs are often displayed in the wrong order.
An obvious solution would be console.log('some flag', JSON.stringify(someObject)), but this makes logs hard to read in a different way. Now I have to copy the stringified object and JSON.parse it somewhere in browser console to make it readable.
What is the right way of writing logs in Firebase and Google Cloud?
The Cloud Functions logger SDK provides a standard interface that has a similar api to console.log statements and supports other log levels. You can use this SDK to log events with structured data, enabling easier analysis and monitoring.
The logger SDK supports log entries as part of a wildcard import. For example:
const functions = require("firebase-functions");
functions.logger.log("Hello from info. Here's an object:", someObj);
For Firebase Cloud Functions is better to use functions:logger to write logs and for reading logs use the command
firebase functions:log from CLI
I'm setting up a dataflow in Google Cloud Platform. I have retrieved data from IoT devices and sent to Pub/Sub, and I now want to store in Firestore. I am using Firebase for my web app where data should be displayed etc.
I realise this should be a simple task, but am still not finding too much information about it (probably too straight forward). Have looked at using Cloud Functions to retrieve from Pub/Sub and add to Firestore, which ought to work (although I got stuck at credentials), but is there an easier way that I am missing?
Your thinking on using Cloud Functions as the recipient of a PubSub message coming from Cloud IoT Core and in that Cloud Function performing an insert into Firestore is exactly correct.
Here is an example showing just that:
https://blog.usejournal.com/build-a-weather-station-with-google-cloud-iot-cloud-firestore-mongoose-os-android-jetpack-350556d7a
If we Google search using "gcp pubsub firestore" we will find many others.
If you use this technique and run into puzzles/problems, don't hesitate to raise new questions (but first search and see if they have been raised before). In your new questions, please be as detailed as possible. There are many samples available for all sorts of related areas. Have a study of these.
I've got 2 different firestore interfaces: both using the same firestore project.
I'm finding this abit confusing - which one am I meant to operate in?
How come the 2nd doesn't have access to other settings such as Rules?
They are both meant for you to operate in. Which one you use depends on whatever your preference is. If you prefer to stay in the Firebase ecosystem, then use the Firebase console. If you prefer to stay in the Google Cloud ecosystem, then use the Cloud console.
Read more about the differences with Cloud Firestore between Firebase and Google Cloud.
Cloud Firestore is available with, or without Firebase SDKs.
For Firebase users, the Firebase interface allows you to configure Firebase specific functionality (Rules).
For GCP users, the Google Cloud interface keeps you closer to other services and admin settings you'll likely be using, such as IAM, BigQuery, etc. It also gives you quick access to a shell (Just click the Cloud Shell icon) so you can quickly run commands like gcloud firestore export.
Both interfaces will show you the same data.
Work in the first one. The second one is a simplified screen I think, because it is also for other services
I am new on firebase cloud functions. I would like to ask a question about always running or self-triggering functions. How can we handle these things? How can we implement always running functions or self-triggering?
Google Cloud Functions are snippets of code that run in response to events that happen somewhere else. Some example events:
a HTTPS URL is accessed, either from application code, or in some other way
a User account is created on Firebase Authentication
a node is written in the Firebase Realtime Database
a message is sent to a Cloud PubSub topic
There is no concept on a forever-running function on Cloud Functions (nor on other, similar Functions-as-a-Service offerings), although it's definitely possible to create a function that gets triggered every minute or so (like a cron job).
In times when Parse.com was on they had a function that called a cloud function directly and returned whatever I wanted. So I could have all the server logic on the server, not in client code. Doe's Firebase has it as well? I can't find it - all I found are HTTP triggers, but it implies that it's not available through Javascript SDK. Am I missing something or do I have to use REST interface for that?
To run server-side code that is triggered by events in Firebase, you'd use Cloud Functions for Firebase. Currently these can trigger code through the Firebase Database updates, Authentication, Analytics, Cloud Storage and HTTP. The documentation I linked has all the details.
As Frank van Puffelen has explained you could use Functions, I'll like to add a couple of things.
You could also use the Firebase Database Admin SDK for the Database, this requires you have a server
Firebase Functions is a sort of big brother, constantly listening for whatever you want. Currently starting and deploying functions is fairly easy and fast, I love this how to video. Basically, you have to install the CLI and then using commands, create a project, write your js for Functions and for deploying those changes to Firebase Functions use the CLI again
Functions can listen more than what the Admin SDK can, the Admin SDK is for the Database while Functions is for, authentification, database, and cloud messaging. This means any user registration or deletion or any change in a node, can trigger further logic. This logic could include sending push notifications. There is a github repo where you can see a lot of examples, I made myself a small repo for the same purpose