1

I'm writing a chat application on android with kt, I have messages saved on firebase realtime database, is it possible to use realtime db persistence to save all messages and be able to read them when user is offline? Because at the moment I tried to enable persistence but when I open the chat I don't see any messages, and I think it might be a memory issue. I read that the real time database cache is 10MB, is it possible to increase it like the Firestore cache? If I can't use cache persistence, do you have any advice on what to use?

For persistence and sync, I use this code in main activity:

 Firebase.database.setPersistenceEnabled(true)
 Firebase.database.setLogLevel(Logger.Level.DEBUG)
        val settings = FirebaseFirestoreSettings.Builder()
            .setCacheSizeBytes(FirebaseFirestoreSettings.CACHE_SIZE_UNLIMITED)
            .build()
        var db : FirebaseFirestore = FirebaseFirestore.getInstance()
        db.firestoreSettings = settings
        val scoresRef = Firebase.database.getReference("chats")
        scoresRef.keepSynced(true)

and i read messages from db with this code:

mDbRef.child("chats").child(senderRoom!!).child("messages")
            .addValueEventListener(object : ValueEventListener {
                @SuppressLint("NotifyDataSetChanged")
                override fun onDataChange(snapshot: DataSnapshot) {

                    messageList.clear()

                    for (postSnapshot in snapshot.children) {
                        val message = postSnapshot.getValue(Message::class.java)
                        messageList.add(message!!)
                    }
                    messageAdapter.notifyDataSetChanged()
                    messageRecyclerView.scrollToPosition(messageAdapter.itemCount - 1)
                }

                override fun onCancelled(error: DatabaseError) {
                    TODO("Not yet implemented")
                }
            })

log that i found

D/WebSocket: ws_2 - WebSocket reached EOF.
D/WebSocket: ws_2 - WebSocket error.
    com.google.firebase.database.tubesock.WebSocketException: IO Exception
        at com.google.firebase.database.tubesock.WebSocketWriter.runWriter(WebSocketWriter.java:159)
        at com.google.firebase.database.tubesock.WebSocketWriter.access$000(WebSocketWriter.java:30)
        at com.google.firebase.database.tubesock.WebSocketWriter$1.run(WebSocketWriter.java:47)
        at java.lang.Thread.run(Thread.java:1012)
     Caused by: java.net.SocketException: Socket closed
        at com.android.org.conscrypt.ConscryptEngineSocket$SSLOutputStream.writeInternal(ConscryptEngineSocket.java:690)
        at com.android.org.conscrypt.ConscryptEngineSocket$SSLOutputStream.write(ConscryptEngineSocket.java:661)
        at java.nio.channels.Channels$WritableByteChannelImpl.write(Channels.java:460)
        at com.google.firebase.database.tubesock.WebSocketWriter.writeMessage(WebSocketWriter.java:138)
        at com.google.firebase.database.tubesock.WebSocketWriter.runWriter(WebSocketWriter.java:152)
        at com.google.firebase.database.tubesock.WebSocketWriter.access$000(WebSocketWriter.java:30) 
        at com.google.firebase.database.tubesock.WebSocketWriter$1.run(WebSocketWriter.java:47) 
        at java.lang.Thread.run(Thread.java:1012) 
D/WebSocket: ws_2 - closed

LeoBe
  • 13
  • 4
  • Did you open the app the first time when you **had** a connection? That is the only way to get data into the cache. That data will then be used when you're offline. – Frank van Puffelen Jan 21 '23 at 17:50
  • yes, i opened the application. I also entered the chat fragment, but when I close the application and disconnect the device, I can't find the messages the next time I open it – LeoBe Jan 21 '23 at 17:58
  • It's hard to say what's going on with more debugging information in that case. Please [create a minimal repro](http://stackoverflow.com/help/mcve), [enable debug logging](https://firebase.google.com/docs/reference/android/com/google/firebase/database/FirebaseDatabase.html#setLogLevel(com.google.firebase.database.Logger.Level)) and then add the code and the log output to the question. – Frank van Puffelen Jan 21 '23 at 18:10
  • I added the code that I think gives the error, and the error log when I open the chat fragment if the device is offline. Thanks for your help – LeoBe Jan 21 '23 at 18:54
  • The code looks fine at first glance, although you don't need any of the lines that say `Firestore` since that is a completely separate database and won't affect the Realtime Database that you are using. --- How much data is under `chats` in your database? The default cache size is 10MB, and instead of trying to increase that I recommend actually trying to keep the size of the cached data under that default as performance is likely to suffer otherwise. – Frank van Puffelen Jan 22 '23 at 01:23
  • I understand, I wanted to use cache to keep all chats saved and have them offline. I don't think 10MB is enough. But at the moment I only have test data, about 100Kb, so for now it should work right? For the future, when data will increase, will a new solution be needed? – LeoBe Jan 22 '23 at 08:21
  • You should not use Firebase Realtime Database's offline cache as a fully offline database. It's a cache of the data that the user has recently seen. So don't cache all message, but cache the ones they're likely to see (e.g. like the first few screens, or all unread and recent ones). --- Switching back to the topic at had though: 100Kb sounds like that shouldn't be causing the problem. But I would expect to a see lot more logging output of what the SDK is loading and caching. Are you sure this is all relevant output from the Firebase RTDB SDK? – Frank van Puffelen Jan 22 '23 at 12:55
  • I found another log that I hadn't seen before: "Database lives in a different region. Please change your database URL to" I added the url with: var ref = Firebase.database(URL), and now it works. Thanks for your help. If I can't enlarge the cache, is it better to use a local database to store messages? for example room with litesql? – LeoBe Jan 22 '23 at 13:17
  • Good to hear you found it. I linked a question where this error came up before, to bring closure to this post. --- It sounds like the problem had nothing to do with caching then, as this would also happen if you didn't want to cache data at all. Going forward, I recommend trying to simplify the code in cases as this, so that you (and us) don't end up chasing red herrings. – Frank van Puffelen Jan 22 '23 at 19:08

0 Answers0