How To Use Event Arc Locally For Cloud Run? - firebase

So I am switching from cloud functions to Cloud Run and I am trying to figure out how to run Event Arc locally. I know you can set up an emulator for Eventarc using Firebase Emulator but Im not sure to have it trigger one of my Cloud Run functions when I write to my local Firestore db. Can someone please let me know how this can be done?
I did see one vague answer here:
Emulation of event-driven design in Cloud Run while developing locally?
But to me this doesn't make sense given that if Im using the local DB and local functions how would a remote instance work with my local dev environment. If this is possible please let me know and how I can accomplish this. Thanks.

It's not an easy task and the team is working to make the local tests easier. For now, I can share my hack.
First of all, you have to know that eventarc is roughtly a wrapper that create several resources behind the scene, especially a PubSub topic and Push subscription to your Cloud Run service. Because of that, an eventarc event is no more than a POST request with the event content as body.
For my hack, I have a Cloud Run service, on GCP, that log the headers and body of any incoming requests. I set up an eventarc with that service as target, and I trigger an event.
I go to the logs, copy the headers and the body of the received event, and I create a curl POST request with that.
Then, when I want to test my local service, I reuse my curl POST request, and submit it to my localhost server.

Related

Execute cloud function from cloud data fusion

I'm trying to trigger the cloud function (with trigger type - HTTP) from cloud data fusion pipeline using http sink plugin version 1.2.2. However I receive the SSL error
java.io.EOFException: SSL peer shut down incorrectly
How do I fix this?
Any help is appreciated, Thanks
To my understanding, it is currently not possible to execute a Cloud Function from Cloud Data Fusion using the HTTP sink plugin. This is because you need an OIDC token which must be generated dynamically during runtime, as they have an expiration date. This is what is explained in this post. As explaind in the post, this token should then be added to the header of the request. To generate this token, you need to run a gcloud auth print-identity-token command, which you cant do using Data Fusion.
The only workaround I see is to Publish a Pub/Sub message at the end of the pipeline to trigger this Cloud Function (however, dont take this as the solution because I would need more context on the precise use-case).

Firebase Emulator Cloud Functions + PubSub subscribe to production topics

There is a service that is publishing messages to my Pub/Sub. Via CLI, I know it is receiving properly the messages.
I want to react correspondingly to those messages. However, I want to develop my subscription, via Cloud Functions, in development environment (firebase emulator), so I won't have to wait 5min between each deploy. But, when using functions.pubsub.topic('topicName').onPublish(...), it won't subscribe to the real prod messages, looks like it will only subscribe to the dev env ones.
I want to, in my firebase emulated Sub/Pub, subscribe to prod messages. Is it possible to do it? How?
Still haven't found an "official" way.
What I am doing for now is use ngrok, get the local function url and then enter it in Pub/Sub Subscription in Push mode. It's a longer way and will require updating the ngrok url for each session (as its url changes in free tier), and also to get the data, JSON.parse(Buffer.from(req.body.message.data, 'base64').toString('utf-8')) and still haven't found a way to auth the JWT auth from request.
But, I can now get the Prod messages from my Firebase Emulator, as I want. You may comment here to ask for further infos about that workaround of mine.

Firebase Remote Config & A/B Testing with real time updates

I've implemented real time remote config updates via the documentation here.
In general, it works as expected, except when it comes to experiments via A/B Testing. Changes to A/B Testing that affect remote config do not fire the update cloud function hook.
Does anyone know if its possible to have the functions.remoteConfig.onUpdate cloud function hook trigger when a change to remote config is made via an A/B Testing experiment change?
The only workaround I can think of is to have a dummy value in remote config itself that I change whenever an experiment is created/updated.
firebaser here
There is nothing built into Remote Config for that at the moment. But thanks to the integration between Cloud Functions and Remote Config, you can build it yourself.
One of our engineers actually just gave a demo for this last week. I recommend you check it out here: https://youtu.be/lIzQJC21uus?t=3351.
In this demo, there are a few steps:
You publish a change from the Remote Config console.
This change triggers Cloud Functions through a functions.remoteConfig.onUpdate event.
The Cloud Function sends an FCM message to all apps through a topic.
When an app receives this message, it shows a prompt that the configuration is out of date.
When the user clicks the "fetch" button, the app fetches the new configuration data from Remote Config.

Is it safe & possible to do almost everything on the cloud side with firebase?

I am using Firebase for my new project. This will be used on both Android and IOS. As you can guess I don't want to write the same code over and over again for both OS.
I am considering to code most of the work with Javascript on the cloud functions. In order to do that I need to use HTTP Requests to call my functions since firebase doesn't support any other way to call cloud functions.
There is two question in my head about this.
Is this possible and does it makes sense?
Since I've been using HTTP Requests all the time isn't that make my app open to listening with tools like Wireshark etc if there are multiple users on the same network? (I know Firebase now supports SSL but do I have to but a domain and license for that?)
What is the best way to do it in an engineer's perspective?
You can certainly move more of your app logic into Cloud Functions. But it's not really true that the only way to invoke a Cloud Function is via HTTP. You can also push data into your database to invoke a database trigger. I gave a talk on this at Google I/O yesterday about how I made a game with all the logic in Cloud Functions. You can watch it here.

Cloud Functions for Firebase based timer

So I know that Cloud Functions for Firebase don't natively support cron jobs, and one have to depend on an external source to trigger an HTTP cloud function. But I don't think this will work in my case.
I have an app in which users can create competitions that end in 24 hours, thus each time a competition is created, I need to set up a timer that triggers one time after 24 hours.
Do you guys have any idea on how this problem can be approached?
At this moment the solution that the Firebase guys are providing for solving the cron issue is to use Google App Engine to deploy a server and trigger the functions via Pub/Sub hooks. Here you can find the tutorial.
Basically you have to:
Create a Cloud Function that responds to a PubSub hook.
Deploy an App Engine that will publish an event depending on the cron setup you set.
If you don't want to setup an App Engine you can use an external resource like cron-job.org that can call your HTTP Cloud Function.

Resources