Is it possible to use Firebase "onWrite" events with client side JavaScript? - javascript

I am building a Cordova mobile app for Android and want to add an event listener to my client-side JavaScript such that a function is called whenever data is written to a section of my firebase database. I know this can be done in node using firebase-functions and the onWrite events. However in trying to follow the getting started documentation (https://firebase.google.com/docs/functions/get-started) for using the firebase CLI, I have hit a roadblock in that I am trying to write client side code, so I cant use
const functions = require('firebase-functions');
because of the "require". I am looking at work-arounds for this like using requirejs but I'm not sure this is appropriate as I don't know which scripts I am trying to load in order for the fire-base functions to work. Is there a better way to do this ? Or is it not appropriate to be trying to use firebase-functions with client side java-script in the first place?
Inexperienced developer so I was hoping there was an easier way then having to make a separate project to use as a server, and a better way than having the client ping the firebase database every second to see if it has updated. Any advice greatly appreciated.

You can't use firebase-functions on the client. It's only for use with backend code deployed to Cloud Functions.
Client code using the Firebase SDK doesn't have the same capabilities as Cloud Functions triggers. That is to say, you can't write client code that triggers the same way as a Realtime Database trigger. You will need to write your client code using the capabilities offered by the client SDK, which involves setting up listeners at locations of the database you're interested in.

Related

execute script on GCP Instance using Google Javascript API

i'm working on dashboard for a customer and i'd like to be able to execute script on my GCP Virtual Machine linux but from the API Javascript.
Already check : https://cloud.google.com/compute/docs/reference/rest/v1/
But there is nothing about manage and execute the script from the VM herself.
Then i'd like to know if someone already did something like this
Just be able to execute "python3 /home/.../MyScript.py" but from the Google API.
Like create html button with onclick and execute the script on the VM with this button, using js-api
(i'm already using the BigQuery API to execute request, and thats work correctly)
Thanks :-)
As of today, you will not be able to do what you are asking for. Nonetheless, I found out two options that might help you as a workaround:
1- On your app server install gcloud and execute shell or system command using your programming language (e.g. PHP).
2- Create a web trigger to execute the script on the GCE instance.
I hope this approach works for you.

Integrating React with Firebase using just react or node.js?

Im new to backend dev, and I have a full fledge working React app that GETs,DELETEs and PUTs data using a fake server. Now I need to use an actual backend and I was thinking of going with Firebase as it seems really convenient. However I saw some examples using Firebase directly in the React app, and some using Node.js to do the work.. Could someone please tell me whats the best way to go with this? If there's an easier way to create REST API using express/mongo, I am also open to those :)
You have to keep in mind that anything you do from the frontend will be visible to the user, so communicating with Firebase from React will be a bad idea at least for anything involving sensitive information (passwords, credit card info, etc.). Just because you can do something doesn't mean you should. Use a Node backend and use that to communicate with your DB and other services.
To address the last part of your question, it is possible that Firebase might be more than you need. A simple setup with Express and MongoDB might be easier. MLab has a pretty good free sandbox database-as-a-service that requires very minimal setup.
I think firebase can also be a good fit, you can use Firebase auth to manage authentication and Rules in the Realtime database or in Firestore to prevent not authenticated users to manipulate your data https://firebase.google.com/docs/database/security/ And to get the best out of Firebase I would recommend using the SDKs but you can also use the REST API https://firebase.google.com/docs/reference/rest/database/
Also if you want to have functionality on the backend you can do so With Firebase Admin SDK https://firebase.google.com/docs/reference/admin/
From my point of view that is one of the advantages of firebase, you can get con going really fast without having to worry about managing infrastructure.

What is the purpose of cloud code on Parse when working with a database?

So my android app is successfully able to add data to the parse database but I am being prompted to deploy some cloud code to start scheduling jobs on my data.
I can query the db effectively from the app through the parse api and so I am very unclear on why I am required to write a javascript program. What is the advantage. The sample on the website is a "hello world" app so I don't understand how this relates to my android app?
appreciate any help
It's the idea of thin client and also the security reasons that leads to using cloud code. And I think it is not mandatory to use cloud code. It is up to you

Can I use PHP SDK and JAVASCRIPT SDK at the same time?

I have a php program/app which asks for input, generate an image and post th image link to user's timeline (processor.php). I used PHP SDK in posting to timeline since I am more familiar with PHP. Now, in my gallery.php, i want to show like buttons for each generated image. I am aware that i have no option but use JS SDK in making button. but is it fine if i use two different SDK's in a single app?
processor.php - PHP SDK
gallerry.php - JS SDK
thanks for your answer! I would love to try your suggetions too
There shouldn't be any problem using PHP and JS in the same application, quite a common use case. You would normally use PHP to create the Web Application structure, handle backend logic, and then use JS to handle UI events.
There's nothing special about combining the two, just create the Web page with JS as normal for the view, and use PHP to build it.
Hope this helps.
Yes, there is no problem, the two APIs are designed to work together. The PHP API is usually used to created the backend logic (for example OAuth implementation, posting and getting data, etc) and the JS API to handle user iniated events.
Also see this: https://stackoverflow.com/a/6728092/1107118 on how to handle login state. Normally the JS API is used for logging in too, and then with the PHP API you take the access token and do the backend actions.

How can I upload a file to dropbox via javascript?

I am trying to allow users to upload large files without tying up my servers for an extended amount of time. I thought using dropbox as file storage might be a good solution. My plan is to use javascript to have the client-side connect directly to dropbox, so that my server is not affected.
I have been trying to find a current javascript dropbox api, but have not had much success. I tried using dropbox-js, but it seems that it is using an outdated version of the API as I get the following error with my current test: {"error": "You're using an older version of the Dropbox API with a new API key. Please use the latest version."}
Does anyone know a fairly simple way to accomplish this task?
Set up your application as a Folder app. If things go wrong, at least you won't blow up people's Dropboxes.
Follow these directions for obfuscating your API key and secret.
Use writeFile to upload the files, and then use makeUrl with the downloadHack: true option, then send the URL to your server.
You'll need the git version of dropbox-js to use downloadHack until the 0.7.0 release comes out.

Categories

Resources