How many socket will open while listening firestore by DocumentReference

536

When you interact with Firebase Realtime Database, a single socket connection between your application and Firebase servers is opened. From that moment on, all traffic between the application and the database goes over that same socket. So it doesn't matter how many times you create an instance of the Realtime Database, it will always be a single connection.

On the other hand, according to @Frank van Puffelen's comment, when you interact with a Cloud Firestore database, the client connects to Firestore using HTTP/2 connections, not web sockets. Both HTTP/2 and web sockets send multiple requests over a single connection.

If there are no active listeners for a period of time, the Cloud Firestore client will automatically close the connection but it will re-open the connection when you attach a listener or perform a read/write operation again.

Share:
536
MSK
Author by

MSK

Updated on December 13, 2022

Comments

  • MSK
    MSK over 1 year

    I am new in Cloud Firestore. When I read the documentation I saw the code below:

    DocumentReference docRef = db.Collection("cities").Document("SF");
    FirestoreChangeListener listener = docRef.Listen(snapshot =>
    {
        Console.WriteLine("Callback received document snapshot.");
        Console.WriteLine("Document exists? {0}", snapshot.Exists);
        if (snapshot.Exists)
        {
            Console.WriteLine("Document data for {0} document:", snapshot.Id);
            Dictionary<string, object> city = snapshot.ToDictionary();
            foreach (KeyValuePair<string, object> pair in city)
            {
                Console.WriteLine("{0}: {1}", pair.Key, pair.Value);
            }
        }
    });
    

    Actually I know how to listen by filtered query and listen all 300 records from a query snapshot, however even if there is only one document update, query reads all records and it increases read count (so does cost) dramatically.

    What if I have 300 documents and I want to listen them all for real time updates by document reference snapshot. Will there be 300 separated sockets or one singleton socket for listening them all. Are C# driver and Flutter driver behavior same ?

    Implementation will be like this;

    foreach (var docRef in docRefList) //300 records
    {
        FirestoreChangeListener listener = docRef.Listen(snapshot =>
        {
            Console.WriteLine("Callback received document snapshot.");
            Console.WriteLine("Document exists? {0}", snapshot.Exists);
            if (snapshot.Exists)
            {
                Console.WriteLine("Document data for {0} document:", snapshot.Id);
                Dictionary<string, object> city = snapshot.ToDictionary();
                foreach (KeyValuePair<string, object> pair in city)
                {
                    Console.WriteLine("{0}: {1}", pair.Key, pair.Value);
                }
            }
        });
    }