RealTime database no triggered

Hi, I’m trying to implement a realtime database for my Table “Message” in Backendless. The same named data class is present in my Android Studio code. this is the class where I implement it

 override fun getDiscussionMessages(discussionId: Long): Flow<List<Message>> = callbackFlow {
        try {
            val eventHandler = Backendless.Data.of(Message::class.java).rt()
            Log.d("debago","eventHandler is $eventHandler")

            val callback = object : AsyncCallback<Message> {
                override fun handleResponse(response: Message?) {

                    Log.d("debago","in get messages, update listener")
                    launch {
                        withContext(Dispatchers.IO) {
                            trySend(datasource.getDiscussionMessages(discussionId).sortedByDescending { it.id })
                        }
                    }
                }

                override fun handleFault(fault: BackendlessFault?) {
                    Log.d("debago","in get messages, update listener ERROR")
                    fault?.message?.let { cancel(message = it) }
                }
            }
            Log.d("debago", "in get discussion message call")
            eventHandler.addCreateListener("discussionId=$discussionId",callback)

            trySend(datasource.getDiscussionMessages(discussionId).sortedByDescending { it.id })


            awaitClose {
                Log.d("debago", "in await close get discussion messages")

                eventHandler.removeCreateListeners()
            }

        } catch (e: Exception) {
            Log.d("debago", "message error discussion message is: ${e.message}")
        }

    }

But I don’t enter, never, in the handleResponse, so my trigger don’t work. Is it an issue with this code?
Thansk you for your help !

Hi, @Indevers

I have created an internal ticket - BKNDLSS-31968. We will look into the issue. As soon as we get any updates on it, we will let you know.

Regards,
Marina

Hi @Indevers

Could you please provide your App ID?

Regards,
Viktor Mudrevsky

Hi @Indevers

I have looked at the Message table in your application and did not find anything that could cause errors. I was unable to reproduce the problem in my application using the exact same table. What errors are you receiving?

Regards,
Viktor

Hi @Viktor_Mudrevsky
My appId is : 6BBA55A8-087F-EBA2-FF1B-596EB5335600
I not have any error, my callbck is just never triggered. I need to refresh the page to see the new message I send. I never see the log : Log.d(“debago”,“in get messages, update listener”)
Did you try to use the same code like me to manage this? Thanks for you support

Hi @Indevers

Thank you for your response.

I have tested the listener on my end using data from your table, and I used my own Java code. The listener triggers when the specified condition in it is met. The code logic should be as follows: first, add the listener, then wait for a new record in the Message table that satisfies the specified condition. Please make sure this is happening correctly on your end. You need to start the listener that waits for a specific discussionId, then send a message with that discussionId. When the message is saved in the Message table in the database, your listener detects this and you end up in the handleResponse method, where you execute further logic.

Perhaps you can use a debugger to check if you are receiving the correct discussionId input and what happens next. Alternatively, you can experiment with the listener’s conditions.

As far as our end is concerned, everything is working as expected in this case, and I currently have no ideas on how else to help you. Please let me know the results of your checks and experiments, or if you have any additional information.

Regards,
Viktor

Ok @Viktor_Mudrevsky , so I have three questions:

  1. the discussionId in my case is a id a create in my table for several messages. Should it be a unique id? Because here this id could be for several lines.
  2. my whereClause is “discussionId=$discussionId”, is is the right format?
  3. can you please send me a screenshot of your code so I can check it?
    Thanks you

Hi @Indevers

  1. In your case, discussionId doesn’t have to be unique.
  2. The format is right.
  3. Here is a screenshot of a simple example in Kotlin. The listener triggered when I created records in the Message table with a value of 2 in the discussionId column.

Regards,
Viktor

We are ok that the Message class in the AsyncCallback is a class you created in your code ? This is mine

Hi @Indevers

Yes, I created the Message class in my code.
In Java, it’s just a class with fields, a public constructor with no parameters, and a public constructor with all parameters.
In Kotlin, I tried to use your class, and as a result, I still ended up inside the handleResponse method. The only difference was that the field values were always the ones you specified by default in the constructor, not the ones I passed in.
Additionally, I tried to create my own class in Kotlin and also tried adding another listener with the condition that discussionId=3. As a result, as you can see in the screenshot, I ended up in the handleResponse method I needed and got the correct data.

Regards,
Viktor

Ok, so I don’t understand why I can’t find myself in the handleResponse. It’s very frustrated. I think I do all as you. I tried to launch this in my repository, and to try too, in the Compose Screen. But no positive result. However the value is well created in my class by the api. I could try to create manually the value to see if the issue is here…

@Indevers

I hope you will find the reason.

On my side, I am closing the ticket BKNDLSS-31968 as not reproducible because, as you saw, the listener correctly triggers incoming data. In this case, the problem is not with the listener’s performance.

Regards,
Viktor

For the moment I have a springboard account, this is linked with my issue? Do I have to setup something specifically in Backendless to activate the real time?

@Indevers

I didn’t configure anything additional, and as far as I know, no additional configuration is needed.
Just tried it in the Springboard plan application, and everything still works for me.

Regards,
Viktor

Ok my last question will be this:

  • Do you launch the eventHandler callback in a coroutine? I’m working with jetpack compose. Where could I launch this? In my repository? In my viewmodel? In my composable Which is the best to work? I think that mayve in my repository, with the callback flow, it could be difficult to handle cause to the awaitClose. I don’t know I try to find anything which could help me…

Hi @Indevers

Let me clarify further. You mentioned that you never see the log:
Log.d("debago", "in get messages, update listener")

What sequence of logs do you see? Do you see the very first one?
Log.d("debago", "eventHandler is $eventHandler")

And what happens next? Do you see:
Log.d("debago", "in get discussion message call")

immediately followed by:
Log.d("debago", "in await close get discussion messages")

Is that correct?

Regards,
Viktor

Yes I see

Log.d("debago","eventHandler is $eventHandler")

And then Log.d("debago", "in get discussion message call")

I only see await close when I left the chat page.
I’m not sure of the Backendless.setUrl. What is the url? In the documentation it seems to be “https://eu-api.backendless.com”. First time, I tried to put my subdomain. Now I put the “https://eu-api.backendless.com” but both don’t works.

So I init my backendless in an Application Class the first time I enter in the app. And then I put my code in my repository. When I send the manage throw the api call with retrofit, I see the created message in my backendless database, but I not enter in my handleResponse. I don’t know if, maybe, the seturl is the cause of that…
How to be spure that I’m really connected to backendless? Is there a way to check that?

@Indevers

Your application is in the North American cluster, according to the documentation, you don’t need to specify Backendless.setUrl. It’s enough to use Backendless.initApp with keys. Please take a closer look at the documentation at Client-side Setup - Backendless SDK for Android/Java API Documentation and make sure that you are connecting correctly.

Also, please add a log for the incoming discussionId value to ensure that it matches the value you are passing.

Could you clarify for me what this method does?
trySend(datasource.getDiscussionMessages(discussionId).sortedByDescending { it.id })

Does it send messages to the chat or save them in the database?

If it saves messages in the database, is there a chance that it saves them before the listener is added? If multiple messages are sent in a row, do they all have the same discussionId or different ones? Do you call this method every time a new message is sent, and a new listener is created each time?

I apologize if my questions seem strange or silly, I just want to help you solve the problem, and I don’t fully understand how your application logic works. I suggest adding more logs to understand exactly what happens and in what order.

Regards,
Viktor

Ok, the application is considered in the North Americain cluster, but is it the server or the real localization? Because I’m located in Luxembourg. Maybe when I created the app, I didn’t pay attention to this information. I don’t know how to change it, and do I need to change it?

I’m trying to implement a tchat inside my app. the getDiscussionMessage(discussionId) go to retrieve (with a GET api call) in the backendless database, all the messages for a specific discussionId between two users and sort them by descending Id to see the last message first.

There is another method in my repository which send the message with a PUT api call. I want to retrieve the messages the first time, subscribe to the listener in this method which retrieve my message, so when I send a message (create one in the backendless database), so I detect it and relaunch the GET to retrieve in my code the last message sent. This is the behavior of this.

I followed the documentation, adding the socket dependency inside my code like this in the gradle

Init my app like this (in the Application class)

And implement my method like this in my repository

I have checked the discussionId value, it’s the good Id (2 in my case)

Hi @Indevers,

We are looking into the matter and will let you know of any updates regarding your issue.

Regards
Nazar

1 Like