# How to use the coroutines-based Ktor websocket client with Android?

**URL:** <https://discuss.kotlinlang.org/t/how-to-use-the-coroutines-based-ktor-websocket-client-with-android/23127>\
**Category:** Support\
**Created:** [October 5, 2021, 9:48pm UTC](https://discuss.kotlinlang.org/t/how-to-use-the-coroutines-based-ktor-websocket-client-with-android/23127 "2021-10-05T21:48:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![AndreKR](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/andrekr/32/8901_2.png) [@AndreKR](https://discuss.kotlinlang.org/u/AndreKR)\
**Post date:** [October 5, 2021, 9:48pm UTC](https://discuss.kotlinlang.org/t/how-to-use-the-coroutines-based-ktor-websocket-client-with-android/23127/1 "2021-10-05T21:48:25Z")

</div>

I’m fairly new to Kotlin. I hope this is on topic here. I’d like to use a Ktor websocket client in an Android app.

I can create a Websocket connection like this:

```kotlin
findViewById<Button>(R.id.buttonTest).setOnClickListener {
  val client = HttpClient(OkHttp) {
    install(WebSockets) {}
  }
  
  runBlocking {
    client.webSocket(method = HttpMethod.Get, host = "127.0.0.1", port = 1234, path = "/websocket") {
      while(true) {
        val othersMessage = incoming.receive() as? Frame.Text
        Log.d("WS", othersMessage?.readText() ?: "<null>")
      }
    }
  }
}

```

It will connect to the websocket and print the first message it receives. (After that it crashes because the connection and thus the channel gets closed, but that’s not the point here. 😃)

Now the part where I don’t know how to plug things together:

The websocket is only available in that lambda that gets passed to `.webSocket`.

But in my Android app I need to open the websocket connection when the activity resumes (possibly handling reconnect, but I’ll worry about that later) keep it open while the activity is active and then be able to call `send()` for example in an onClick handler of a button. When a message comes in at any point I need to be able to handle it, for example by logging it (for now).

---

<div class="post-metadata">

**Author:** ![bgrebennikov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/bgrebennikov/32/10325_2.png) [@bgrebennikov](https://discuss.kotlinlang.org/u/bgrebennikov)\
**Post date:** [April 17, 2023, 1:35pm UTC](https://discuss.kotlinlang.org/t/how-to-use-the-coroutines-based-ktor-websocket-client-with-android/23127/2 "2023-04-17T13:35:44Z")

</div>

you must use a websocket sessions. also i can see infinity loop in UI thread, thats not good…  
check this code example:

ChatRepository: [ChatRepository.kt · GitHub](https://gist.github.com/bgrebennikov/0b11c551a3c0790388b587e6597d2287)  
ChatRepositiryImpl: [ChatRepositoryImpl.kt · GitHub](https://gist.github.com/bgrebennikov/aad21a83b326eca1be17f8eb892544f8)  
MainActivity: [MainActivity.kt · GitHub](https://gist.github.com/bgrebennikov/c20876054b3fa6860fe1d3e3e3eeca09)

hope it will helps you 🙂
