2

I am using the Firebase Realtime database REST API to stream events from the server when data gets uploaded (See https://firebase.google.com/docs/reference/rest/database#section-streaming-cancel)

However, when I start the event stream, it always downloads the whole tree. I do not need that, I only want to receive live changes. Example:

GET https://[PROJECT_ID].firebaseio.com/database.json

// I want to get rid of this event
event: put
data: {"path": "/", "data": {...}}  // very large JSON tree

// I want to keep subsequent events, to entries changed in that tree:
event: put
data: {"path": "/entry42", "data": {...}}  // small JSON tree, ok

Is there any way to tell the database to skip the first event? I do not need this data (as a get the current state in a different, much more efficient way). I only want live changes to data.

Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
Felix
  • 6,885
  • 1
  • 29
  • 54

1 Answers1

2

Realtime Database doesn't have a sense of "only things that changed since the last time I queried". That's just not a query you can make. What you can do instead is use something like a timestamp to determine what's new, and query the database using that timestamp to find only nodes that are newer than the last timestamp that you used for the prior query.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • I am aware of that and tried to do such a query. However, using "orderBy" (and the other related query parameters) always leads to an `Error 400 - bad request`. It works for normal GET requests, but that's not what I am asking for here. – Felix Jan 05 '20 at 19:40
  • It actually does work! There was an error in my test case - using queries works for event-streamed data just as for a normal GET request. Thank you sir! – Felix Jan 08 '20 at 11:16
  • @Felix do you happen to have an example of how you start a stream and limit it using orderBy? – foamy May 03 '21 at 11:38
  • @foamy Just like any normal request. Build your HTTP request just as you would for a normal GET and set the `Accept`-Header to `text/event-stream` and `Cache-Control` to `no-cache`. – Felix May 05 '21 at 10:56