Send data to Firebase

I’d like to send data from my Electron device to Firebase. But it seems it is difficult.

I have two choices.

1, use Webhook.

But I need to send data to the following url:

https://test-408c1.firebaseio.com/b.json

I’d like to change ‘b.json’ according to the device id. But I haven’t found any solution to change the URL of a Webhook in runtime.

2, use Http client.

The Httpclient library (https://github.com/nmattisson/HttpClient) seems very basic. I even don’t know how to set ‘https’.

Any information will be appreciated.

1 Like

I still would use a web hook, as the HttpClient does not support https and while there is a https client, it’s kind of hard to use.

In order to change the parameters in a GET request, you use the Query Parameters part of the web hook. The data can either be fixed in the web hook, or parsed from the data sent from the Photon/Electron and caught by the web hook. You can also send multiple values from the Photon/Electron, formatted in JSON, and parse out the individual pieces so you can put them into your GET query parameters or POST data as needed for the service you’re interacting with.

I need to change URL not Query Parameters. My URL:

https://test-408c1.firebaseio.com/b.json

I need to change ‘b.json’ in runtime.

Sorry, I misread that. I’m pretty sure you can’t put a mustache template in the URL field so, yes, that would indeed be a problem.

Ok this is going to be a fun project.

Disclaimer: I have 6 days and counting worth of experience with Particle. However I have 4 years of experience working with Google APIs and Googles Authentication servers. Me

I have been trying over the last few days to get webhooks to send data to Google Analytics which does not require any form of authentication. Current status kind of works. So I do know a little about webhooks and how they work. I am basically replying to this because I can fill in some of the gaps from the FireBase and I am happy to help. As far as i know you cant edit a webhook once its been created the endpoint is going to have to be static

As already stated

HTTPS is required. Firebase only responds to encrypted traffic so that our data remains safe.

Sending data.

I have been scanning the documentation it seams that you can send data to them with out being authenticated. This seams to be wrong in my opinion. You should check up on it i would assume that when you send data to an account they would require that you be authenticated.

Apparently they do allow for authentication. Now for some depressing news it seams their authentication method is JWT this is basically service account authentication. This means your not going to be able to just have a token on your device and request access like you could with normal Oauth2. Your going to have to calculate the JWT (Which I personally have never managed to do with Googles Auth servers with out using their official client libraries) Granted i think Googles Oauth servers are slightly different then Firebases but its probably the same bread of server.

Before you go to deep into this i would find out.

  • Do you have to be authenticated to send data?
  • If you have to be authenticated can you create a JWT with your device.

If you don’t need to be authenticated then I can probably help you put together the JSon required to send data to the server from the info in the documentation.

It is easy to pass the authentication. Just append the secret key as a query parameter.

Tokens have to be generated when you send them using the Secret. Because it has current time built into the JWT. Can you generate the Token on the device?

I guess i could be miss reading there documentation and they have another form for authentication then JWT. I will have a look around.

BTW I used the HTTP library it was quite easy to set up. If you need to change the endpoint might be best to go with that since you cant edit webhook after it was created.

The HTTP library can’t support HTTPS. But that is an HTTPS library. I am going to test it.

1 Like

Please let me know. Sounds like a fun project and i am sure you wont be the only one wanting to use firebase.

Sure. I will

1 Like

I wrote a proxy to tackle this problem, since Firebase v3 requires JWT tokens that are difficult to generate properly. This proxy will authenticate your device_id and particle access token (so that it can send back realtime database events, and provide some layer of authentication without you having to do your own device authentication in firebase) and generate a Firebase auth object where the uid matches the device_id, so you can lock down Firebase trees with server rules that match auth.uid. Something like:

https://mybase.firebaseio.com/device/my_device_id.json

would be only readable to your actual device (where the proxy authenticates your particle token and device id before creating the firebase connection), assuming you set the rules to:

{
"rules":{
  "device" : {
    "$device_id" : {
      ".read" : "auth.uid === $device_id"
      }
    }
  }
}

It’s alpha until I can test it on a “real world” application. I haven’t tested the webhook example, and I used a mustache template in the URL field which… I guess might be wrong. If that doesn’t work, I’ll rework the proxy for a convenient way to pass the device_id, maybe in the headers.

Hi @anderson916 I am in the same boat as you. We are trying to send event data from Photon board to Google Fire-base Real-time DB. If you find out anymore information on it or have already figured it out please let me know. Maybe if you haven’t found a method yet we can team up.

I haven’t found a direct way to do that. Currently I am using my web server to receive data from the Particle and send data to Firebase.

I figured out how to read and write Google Firebase from webhooks without your own server. The tutorial is here: