Unofficial Anvil Firebase Integration v0.1🔥

@mark.breuss Do you also have a custom wrapper for the server-side python sdk (reading between the lines from an old post of yours)? I would be interested in that, also, for one. I’ll share my own server-side “hello world” in a separate thread.

I would not call it a wrapper, generally you can just use the official python sdk in anvil as is. Get started with Cloud Firestore  |  Firebase

I have some helper functions that enable authenticating to firebase with anvil users and so on but nothing major. Maybe the basic functions could be included in the anvil-firebase dependency for ease of use.

1 Like

Mark, I’ve noticed now that including your firebase third-party dependency in my app slows down server calls by around 3 seconds (without persistent server enabled). I’m guessing this is because the dependency is importing some things in a server module (perhaps firebase_admin) even though the server is not needed for the documented features of the dependency, as far as I understand. If I’m right about this, could you actually remove the server module from the dependency?

Hi @hugetim,

yes - thats mos likely whats going on here.
I just changed it to be lazy loading and your cold starts should be fine now.

Its currently only on the development / published branch.
Will try to do some test and push a proper version out later this day.

Mark

Update:
I published the above updates in a new version 0.6

1 Like

Great, thanks! That does fix the loading issue for me.

1 Like

I haven’t gone right into the docs of this yet, but are push notifications something we can do now/on the horizon?

1 Like

Currently this wrapper exposes the following firebase services: authentication, firestore, storage and cloud functions.
Firebase Notifications would be relatively straight forward to add to this dependency, so it is definetly on th horizion.

2 Likes

Thanks for that. I actually tried firebase before with Anvil and it seems to me Firebase replaces all the Anvil backend as only front-end Anvil is used.

Besides using JS instead of Python, might I ask the reasons you don’t use firebase directly with Anvil front-end ?

Hi @Tony.Nguyen,

not sure if I understand you correctly but this library interacts directly with anvil frontend. It is simply a Javascript wrapper.

So yes with this a serverless approach is possible where no server module is needed.

Sorry for my bad wording, so your wrapper makes it easy to work with firebase using Python syntax only? And it is the main reason to use the wrapper instead of pure firebase javascript?

One advantage is code consistency, autocompleter and some helper functions, the main thing however is serialization.

This dependecy, amongst other types serializes datetimes directly to javascript and back. Which you would ned to take care of yourself otherwise.

We have had all code in javascript previously and it was quite the mess to be honest.
But of course there is nothing against writing it in pure javascript. :+1:

3 Likes

Hi Mark. I am trying to upload a pdf file to my firestore database but I keep getting an error. I can upload text and numerical values fine, but I get an error when I upload a file from a FileLoader.
Here is my sample code.

name = self.textbox_name.text
surname = self.textbox_surname.text
email = self.textbox_email.text
number = self.textbox_number.text
pdf_file = self.file_upload_pdf.file

new_person = {'Name':name, 'Surname':surname, 'E-mail':email, 'Number':number,'File': pdf_file}
firestore.set_doc(self.doc_ref, new_person )

error message:
ExternalError: FirebaseError: [code=invalid-argument]: Function setDoc() called with invalid data. 

Hi @junior,

if you want to upload a pdf file you need to use firebase storage.
firestore is a document only database not suitable for blob media files.

You will want to use storage for that - which is optimized for such data.

# define a storage path
ref = storage.ref('images/test1.png')
# upload file
storage.upload_media(ref,file)
#optionally get url of the media obj -> can be used for image components
url = storage.get_download_url(ref)

Your workflow could be:

  1. upload pdf file
  2. store reference or path on your person document.
1 Like

Hi Mark. Thank you for this. It works.
How can I download the pdf from firebase storage to anvil onto my local machine?
This was my assumption but it didn’t work.

doc_ref = fs.doc(fs.db, 'collection_name','someuid')
doc_uid,doc_data = fs.get_doc(doc_ref)

ref = storage.ref(doc_data['pdf_name'])
url = storage.get_download_url(ref)

download(url)

Try to get it to a media object which has a download method:

 file = anvil.URLMedia(url)
 file.download()

This would be something that could be built into the sdk however. I’ll take it on to out list :+1:

2 Likes

Thank you so much Mark.

1 Like

Thanks for asking the question everyone wanted to know but was too afraid to ask themselves.

1 Like

Potientially noob question - how are you securing your API key? If you’re authenticating Firebase in client code, doesn’t that expose your API key? Or does each end user have their own authentication credentials tied to their user account?

1 Like

hope it helps:
Unlike how API keys are typically used, API keys for Firebase services are not used to control access to backend resources;

Firebase authenticates users using a variety of methods, including:

  • Email and password: This is the most common method of authentication. Users create an account with their email address and password, and then use those credentials to sign in to your app.
  • Social login: Users can sign in to your app using their social media accounts, such as Google, Facebook, or Twitter.
  • Custom authentication: You can also implement your own custom authentication system. This is useful if you need to store additional user data, such as a username or phone number.

https://firebase.google.com/docs/projects/api-keys

3 Likes

I demo a form of custom auth integrated with Anvil’s Users Service here: Server-side Firebase tutorial

1 Like