Real time messaging subscription in kotlin

Hi, I am having converting this to kotlin from your java doc example. Can you translate to kotlin?. Got it working for the custom class approach but not the dict/map. Regards, Mike

final Channel channel = Backendless.Messaging.subscribe( “demo” );
channel.addMessageListener( new AsyncCallback()
{
@Override
public void handleResponse( HashMap personObject )
{
Log.i( “MYAPP”, "Received a message " + personObject );
Log.i( “MYAPP”, "name - " + personObject.get( “name” ) );
Log.i( “MYAPP”, "age - " + personObject.get( “age” ) );
}

@Override
public void handleFault( BackendlessFault fault )
{
Log.e( “MYAPP”, "Error processing a message " + fault );
}
}, Person.class );

Hi @mike-turner ,

Did you have any errors in your Android application logs?

Regards, Andriy

Hi @Andriy_Konoz ,

No errors because I cannot figure out how to write the Kotlin code (not that familiar with Kotlin) . Android studio translates above java code to this…

val channel = Messaging.subscribe("demo")
channel.addMessageListener(object : AsyncCallback<HashMap<*, *>> {
    override fun handleResponse(personObject: HashMap<*, *>) {
        Log.i("MYAPP", "Received a message $personObject")
        Log.i("MYAPP", "name - " + personObject["name"])
        Log.i("MYAPP", "age - " + personObject["age"])
    }

    override fun handleFault(fault: BackendlessFault) {
        Log.e("MYAPP", "Error processing a message $fault")
    }
}, MessageObject::class.java)

but then complains with the line channel.addMessageListener(…)

None of the following functions can be called with the arguments supplied.
addMessageListener(AsyncCallback<TypeVariable(T)!>!, Class<TypeVariable(T)!>!)
where T = TypeVariable(T) for fun <T : Any!> addMessageListener(callback: AsyncCallback<T!>!, clazz: Class<T!>!): Unit defined in com.backendless.rt.messaging.Channel
addMessageListener(String!, AsyncCallback<String!>!) defined in comm.backendless.rt.messaging.Channel
addMessageListener(String!, MessageInfoCallback!) defined in com.backendless.rt.messaging.Channel

Soi basically I just don’t know how to convert the java supplied example to kotlin if that makes sense?

@mike-turner ,

It seems that example itself is not completely correct.
Channel.addMessageListener method receives as the second argument type of object with which AsyncCallback should work. In example provided by you, you want to handle instances of Person class while callback itself expects HashMap.
Java compiler wont compile such code. So I assume that the same error encountered Kotlin compiler.
Correct Java code with Person class will look in the next way:

    final Channel channel = Backendless.Messaging.subscribe( “demo” );
    channel.addMessageListener( new AsyncCallback()
    {
      @Override
      public void handleResponse( Person personObject )
      {
        Log.i( “MYAPP”, "Received a message " + personObject );
        Log.i( “MYAPP”, "name - " + personObject.getName() );
        Log.i( “MYAPP”, "age - " + personObject.getAge() );
      }

      @Override
      public void handleFault( BackendlessFault fault )
      {
        Log.e( “MYAPP”, "Error processing a message " + fault );
      }
    }, Person.class );

For this variant you also should provide definition for Person class.

Correct Java variant with HashMaps will be the next:

    final Channel channel = Backendless.Messaging.subscribe( “demo” );
    channel.addMessageListener( new AsyncCallback()
    {
      @Override
      public void handleResponse( HashMap personObject )
      {
        Log.i( “MYAPP”, "Received a message " + personObject );
        Log.i( “MYAPP”, "name - " + personObject.get( “name” ) );
        Log.i( “MYAPP”, "age - " + personObject.get( “age” ) );
      }

      @Override
      public void handleFault( BackendlessFault fault )
      {
        Log.e( “MYAPP”, "Error processing a message " + fault );
      }
    }, HashMap.class );

Could you please try one of the suggested variants and write me back?

Regards, Andriy

Generated Kotlin version for variant with HashMaps was correct in my case:

 val channel = Backendless.Messaging.subscribe("demo")
        channel.addMessageListener(object : AsyncCallback<HashMap<*, *>> {
            override fun handleResponse(personObject: HashMap<*, *>) {
                Logger.getLogger("MYAPP").info("Received a message $personObject")
                Logger.getLogger("MYAPP").info("name - " + personObject["name"])
                Logger.getLogger("MYAPP").info("age - " + personObject["age"])
            }

            override fun handleFault(fault: BackendlessFault) {
                Logger.getLogger("MYAPP").error("Error processing a message $fault")
            }
        }, HashMap::class.java)

I replaced your logger class with Backendless logger for this example

Hi @Andriy_Konoz

Many thanks this worked, Java and Android

The Java one I replaced
channel.addMessageListener( new AsyncCallback()

with

channel.addMessageListener( new AsyncCallback<HashMap>()

All up and running now I think, many thanks for your help.

1 Like