简体   繁体   中英

Firebase Xamarin.Forms - too many concurrent connections

currently I'm developing an App with a small team using Xamarin.Forms. We need to communicate with a database to get some locations, order details and so on. We are using Google's Firebase (Realtime DB) for this purpose. Everything is working fine when we are writing and reading data. However in the Firebase Console, in the usage tab, there are over 50 concurrent connections . This is weird since we are currently developing and didn't release any version of our app. There should be at most 5 concurrent connections (we are a team of 5).

We are using the NuGet-Package FirebaseDatabase.net (4.0.4) https://www.nuget.org/packages/FirebaseDatabase.net/ to read and write to the database.

Multiple Listeners are used to be able to react to changes in the database (so far it seems that each listener is taking up one connection which doesn't seem to be correct).

The code below shows the initialization of the FirebaseClient which is called once in the constructor.

private FirebaseClient InitDbClient()
{
    var dbClient = new FirebaseClient(Constants.Values.FIREBASE_DATABASE_URL, new FirebaseOptions()
    {
        AuthTokenAsyncFactory = () => Task.FromResult(_authToken)
    });
    return dbClient;
}

Each listener is implemented in a similar way to the following code:

public IDisposable SubscribeToChatMessages(string orderID)
{
    var observer = _dbClient.Child($"orders/{orderID}/Chat/Messages").AsObservable<JObject>();

    var subscribe = observer.Subscribe(t =>
    {
        if (t.EventType == Firebase.Database.Streaming.FirebaseEventType.Delete)
        {
            return;
        }

        ChatMessage msg;
        try
        {
            msg = JsonConvert.DeserializeObject<ChatMessage>(t.Object.ToString());
        }catch(Exception e)
        {
            Debug.WriteLine(e.Message);
            msg = new ChatMessage() { C = null, T = new DateTime(), U = null };
        }

        //...do something with the chat message
    });
    return subscribe;
}

Since I'm not sure what the problem is I just put some of our code in here. It would be awesome if anyone has a solution for this problem or has any idea what we might try.

I found the answer myself. As I already suspected each listener uses one connection. The reason for that is simple: the package is built on top of the rest api of firebase.In some other question it was mentioned that each listener is basically a streaming web socket (or something like that). Each of these consume one of the concurrent connections.

As a workaround (to reduce the amount of concurrent connections) I replaced all listeners that are not actually necessary with a combination of a timer and a simple db request. To be able to query for new data I added timestamps to the data itself. This allows me to use a db query similar to

dbClient.Child($"orders/{orderID}/Chat/Messages").orderBy("Time").startAt(lastRead).OnceAsync<>()...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM