Firebase SSE send whole json file when the connection Opened

Viewed 166

I'm trying to implant Firebase SSE for a chatroom in Unity game (for WebGL/Mobile), with a library "BestHTTP", in order to mimic the "child_added" or "child_changed" listener in Firebase's Javascript SDK, that only the newly added or updated data will be send to the client.

My problem now is that when the connection is initialized and opened, the "put" listener will always sent back a fat JSON, including all the data in my target firebase node. After the open event, the listener works as expected. Only updated data will be send back to client via "put" listener.

I'm new to SSE, and wondering if this is a standard SSE behavior, or it's determined by Firebase's own rule, or a problem from my code/library?

Firebase REST SSE: https://firebase.google.com/docs/reference/rest/database#section-streaming

void Init(){
    var eventSource = new EventSource(new Uri("https://{}.firebaseio.com/TestChatRoom.json"), 1);
    eventSource.On("put", OnPut);
    eventSource.Open();
}
void  OnPut(EventSource source, Message msg){
    DebugLog(string.Format("OnPut: <color=yellow>{0}</color>", msg.Data.ToString()));
}

==========Update==========

Solved this with @Frank's help. Turns out I can use the same query parameters in Firebase's RestAPI for SSE listener. All I need is adding "orderBy="$key"&limitToLast=20" in my request url, so it will only return the latest 20 child after the connection is opened, and keep updating when new child is added.

Here are the code works for me (C# for Unity, with plugin BestHTTP):

void Init(){
    var eventSource = new EventSource(new Uri("https://{}.firebaseio.com/TestChatRoom.json?orderBy=%22$key%22&limitToLast=20"), 1);
    eventSource.On("put", OnPut);
    eventSource.Open();
}
void  OnPut(EventSource source, Message msg){
    Debug.Log(msg.Data.ToString());
}
1 Answers

What you're seeing in the REST streaming response is actually how all Firebase clients (including all SDKs) work. Firebase synchronizes the state of the node that you subscribe to, it does not just pass events that happen after the subscription starts.

If you only want to get new data, you'll have to query for what your app considers "new". Typically this means one of these approaches:

  • Keep the key of the most recent node you've seen somewhere in local storage on the client, and then OrderByKey().StartAt("thatKey").
  • Store a timestamp in each child node, and then either start at the last timestamp you've seen, or start at now.
Related