Kotlin headers for mqtt javascript library


I’m trying to create a wrapper for Paho Mqtt Javascript library.

This is a really good simple library. I had success creating a wrapper for it in Dart.
Now I’m migrating my project that make use of it to kotlin.

This is an example on instantiating a new object of Client in js realm.

client = new Paho.MQTT.Client(location.hostname, Number(location.port), “clientId”);

I read the documentation about the external keyword; but notice the nested object: Paho.MQTT.Client

Any suggestion?

This is the library https://github.com/eclipse/paho.mqtt.javascript/blob/master/src/paho-mqtt.js
There you will find one usage example in the firsts lines of comments (line 58)



Maybe a solution could be @Platformname annotation!

I’ll try it!


HI! This:

external object Paho {
    object MQTT {
        class Client(hostname: String, port: Int, clientId: String) {
            val hostname: String
            val port: Int
            val clientId: String

fun main(args: Array<String>) {
    val c = Paho.MQTT.Client("localhost", 8080, "me")

roughly translates to

var Paho$MQTT$Client = Paho.MQTT.Client;
var println = Kotlin.kotlin.io.println_s8jyv4$;
function main(args) {
    var c = new Paho$MQTT$Client('localhost', 8080, 'me');
    println(c.clientId + '@' + c.hostname + ':' + c.port);

Is that what you need?

Please note, that at the moment properties in the external class constructors are prohibited.


Yes, exactly what I needed! Thank you Anton, you put me on the right road :smile:

Now my porting is almost complete!
In the case this may help others, I paste here my paho header

 package utils

external object Paho {
    object MQTT {
        class Client(hostname: String, port: Int, clientId: String) {
            val hostname: String
            val port: Int
            val clientId: String
            fun connect(options: Options)
            fun send(message: Message)
            fun subscribe(destination: String)
            fun disconnect()
            var onMessageArrived: (msg: Message) -> Unit
            var onConnectionLost: (response: Response) -> Unit

        class Message(payloadString: String) {
            val payloadString: String
            var destinationName: String

        class Response {
            val errorCode: Int
            val errorMessage: String

        interface Options {
            var userName: String
            var password: String
            var reconnect: Boolean
            var useSSL: Boolean
            var onSuccess: (response: dynamic) -> Unit
            var onFailure: (response: Response) -> Unit

object PahoTest {
    fun tryit(conf:Uri) {
        val client = Paho.MQTT.Client(conf.hostname, conf.port, conf.params["clientId"]!!)
        val opt: Paho.MQTT.Options = js("({})")
        opt.userName = conf.username
        opt.password = conf.password
        opt.onSuccess = {
            client.send(Paho.MQTT.Message("debug: hello world").also { it.destinationName = "destination2" })
        opt.onFailure = { println("FAILURE ${it.errorCode} ${it.errorMessage}") }
        opt.reconnect = true
        opt.useSSL = true

        client.onMessageArrived = { println("payload=${it.payloadString}") }



Great job! =)

By the way we’ve just set up an external declaration repository.
It is more of an EAP at the moment, but those declarations are available in NPM, Maven and Gradle.

Would you be interested in checking it out and potentially adding your declarations via a pull request?
You feedback would be valuable =)


Shouldn’t a link to this repository maybe be part of the Calling Java from Kotlin document?


Great project!
I just did a pull request.

Just a question about the code. I’m not very happy with this:

val opt: Paho.MQTT.Options = js("({})")

Is there a better way to get an instance of Options?
Options is just a javascript object…

thanks in advance.


It will. Consider it to be a EAP. =)

We’d like to receive some early adopters feedback first. And maybe add some more declarations before advertising it as well. This way we make sure it works and is actually useful. =)


I’m not aware of any ‘standard’ way. Personally I fancy this solution


Yes, that’s neat!