1
votes

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
Any reason why you are using the REST API instead of the provided client library for unity? - Doug Stevenson
It seems REST is the only way to work across mobile/webgl/editor. It's a pain... The official library is focused on android/ios. - Ziii
Well, you might want to edit the question to explain in more detail what exactly isn't working the way you expect, along with your debugging details. We can't see inside your database or observe the execution of your code. - Doug Stevenson
Thanks. Added more description about the expected result. Will add debug log later on. - Ziii
Hey @Ziii. Did you get anywhere further with this? If my answer was useful, click the upvote button (▲) to the left of it. If it answered your question, click the checkmark (✓) to accept it. That way others know that you've been (sufficiently) helped. Also see What should I do when someone answers my question? - Frank van Puffelen

1 Answers

2
votes

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.