Ok, so I did a little bit of reverse engineering and found out how to work with the webSocket in the realtime db for streaming.
First I want to explain why to build my own js-SDK, why not just use firebase js-SDK:
Firebase is amazing (no, really amazing), but there SDK is huge... and not everyone need all the features that firebase provide.
By useing webpack-bundle-analyzer, I notice that half on my main bundle is just the firebase SDKs:

And by building my own libs for firestore and realtimeDB I reduce the first loaded bundle size almost by half (from 4.04Mb to 2.4Mb)
Let's start:
Init the socket:
// not sure where to find the databaseId - i just copy it from the firebaseSDK
new WebSocket("databaseId");
sent first auth:
t : "d",
d : {
r : socketIndex_n++, // everyMsg increment
a : "auth",
b : { "cred" : userIdTokcet} // explained in firebase docs, how to get the token
}}
start listening for node in the json tree:
t : "d",
d : {
r : socketIndex_n++, // everyMsg increment
a : "q",
b : {
p : "/parent/child1/child2",
q : {}, // query explain below
t : socketIndex2_n++, // everyStreamingMsg increment
h : ""
}
}
}
stop listening for node in the json tree:
t : "d",
d : {
r : socketIndex_n++,
a : "n",
b : {
p : "/parent/child1/child2",
q : {}, // query explain below
t : socketIndex2_n, // equal to the everyStreamingMsg
}
}
}
Query:
switch(queryName) {
case "orderBy":
ans_o.i = val;
break;
case "startAt":
ans_o.sp = val;
break;
case "endAt":
ans_o.ep = val;
break;
case "limitToFirst":
ans_o.l = val;
ans_o.vf = "l";
break;
case "limitToLast":
ans_o.l = val;
ans_o.vf = "r";
break;
}
Good to know:
- Send every 45 seconds a msg with 0, to keep the connection live
If some one from Firebase see this (first you are amazing), and please if you could update you docs about it, it could be really helpful.