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());
}