0
votes

I would like to implement realtime streaming from firebase realtime database.

I started by using EventSource as describe here: https://firebase.google.com/docs/reference/rest/database#section-streaming, but when I try to open more then 6 connections, my requests got blocked, meaning I could only listen to 6 endPoints simultaneously.

I did a little bit of a research and I found out that firebase open a webSocket, to send back and forth information, and part of that information is the path to the nodes of the "firebase realtime database tree" to listener for changes. (How firebase listener actually work?)

Is there any way to access the websocket to send my own requests?

I cant find any doucments about it.

thank you!

2
Please. if you dont like my question, write your reason, thank you - dan

2 Answers

1
votes

firebaser here

The Web Socket wire protocol for the Firebase Realtime Database is not documented.

That said: the REST streaming protocol should allow you to get pretty close to the same behavior.

The Firebase server should not be blocking the REST connections though, as there are plenty of projects that have many more connections through this protocol. If you are unable to open more than 6 connections, the problem is likely between the devices opening those connections and the server. You might want to try on a different network, on different machines, and possibly in different regions.

If you set up a reproduction on a site like jsbin, I'll have a look if it works for me.

0
votes

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:

enter image description here

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:

  1. Init the socket:

    // not sure where to find the databaseId - i just copy it from the firebaseSDK
    new WebSocket("databaseId"); 
    
  2. 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
    }}
    
  3. 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 : ""
            }
        }
    }
    
  4. 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
        }
    }
    }
    
  5. 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.