Java client usage on Android with Kotlin

Iván Ferro — May 15, 2021

Java Client Usage on Android with Kotlin


To use the official library in Android we need to add the dependency to the build.gradle file at Module level. Please use the latest released version, which at this writing is 2.11.2

dependencies {
    //other dependencies
    implementation 'io.nats:jnats:2.11.2'


We will create a class as a manager to control our NATS client to be able to connect, disconnect, publish … This is necessary because when tries to connect, it makes a blocking call, which we don’t want in the main thread, so we execute it in another thread. Also, in order to have communication between the main thread and connect thread, we generate an interface that we will implement in the MainActivity and that we pass as parameter to our class NatsManager. In this case, we will make that the interface returns us when we are connected and what messages it revokes.

open interface IDataCollector {
    fun setConnect(connect: Boolean)
    fun setResponse(response: String)

class MainActivity : AppCompatActivity(), IDataCollector {

    override fun onCreate(savedInstanceState: Bundle?) {
        val hide = supportActionBar?.hide()


        text = findViewById(
        text.text = responses

        nats = NatsManager(this)

        var btn : MaterialButton = findViewById(
        btn.setOnClickListener { nats.connect() }

        var btn2 : MaterialButton = findViewById(
        btn2.setOnClickListener {"test", "THIS IS A TEST MSG") }

   // . . . 

    override fun setConnect(connect: Boolean) {
        val natsGreenBar: LinearProgressIndicator = findViewById(
        if(connect == true){
            natsGreenBar.visibility= View.VISIBLE

    override fun setResponse(response: String) {
        responses = responses+response+"\n"
        text.text= responses


class NatsManager(datacollector: MainActivity) {

    val TAG = "Nats Service"
    lateinit var nc : Connection
    val datacollector = datacollector

    // . . . 

In this example we are going to create a method connect that is going to connect to the server with the options that we indicate. Once connected it is going to subscribe to the subject test. As we said before, we need the instruction Nats.connect() inside a new thread. Initializing from this the variable nc, being thus accessible from the main thread.

 fun connect() {
        Log.d(TAG, "TRY TO CONNECT")
        Thread {
            val options: Options = Options.Builder().server("nats://").build()

            try {
                nc = Nats.connect(options)
                Log.d(TAG, "Connected to Nats server ${options.servers.first()}")
                connect= true

                val d = nc.createDispatcher { msg: Message? -> }

                val s = d.subscribe("test") { msg ->
                    val response = String(, StandardCharsets.UTF_8)
                    println("Message received (up to 100 times): $response")

            } catch (exp: Exception) {
                connect = false


To create a publication method we just have to call the function, passing as parameters the subject and message in ByteArray.

fun pub(topic: String, msg: String){
        nc.publish(topic, msg.toByteArray(StandardCharsets.UTF_8))
        Log.d(TAG, "Published msg ${msg} on topic ${topic}")

In this way we have a functional client in Android using the official NATS Java library in a very simple way. You can check the complete code in Github .

About the Author

Iván Ferro is a member of research and development team COM at University of Vigo .

Back to Blog