# Using OPENRNDR as a websocket client

**URL:** <https://openrndr.discourse.group/t/using-openrndr-as-a-websocket-client/532>\
**Category:** How to?\
**Created:** [March 17, 2023, 7:30pm UTC](https://openrndr.discourse.group/t/using-openrndr-as-a-websocket-client/532 "2023-03-17T19:30:51Z")\
**Posts on this page:** 1\
**Showing post:** 9

<div class="post-metadata">

**Author:** ![abe](https://yyz2.discourse-cdn.com/free1/user_avatar/openrndr.discourse.group/abe/32/700_2.png) [@abe](https://openrndr.discourse.group/u/abe)\
**Post date:** [March 22, 2023, 5:49pm UTC](https://openrndr.discourse.group/t/using-openrndr-as-a-websocket-client/532/9 "2023-03-22T17:49:02Z")

</div>

Ok I think this is much cleaner, with the WebSocket listening outside the draw loop.

There’s no need to keep creating [Coroutines](https://kotlinlang.org/docs/coroutines-overview.html) inside `extends { }`. Just create one with an infinite loop that waits for new messages to arrive. It seems to automatically sleep when there’s nothing received.

```auto
fun main() = application {
    val client = HttpClient(CIO) {
        install(WebSockets) {
            pingInterval = 10_000
        }
    }

    program {
        val session = client.webSocketSession(HttpMethod.Get, "localhost", 8080, "/")
        val radius = AtomicInteger(140)

        GlobalScope.launch {
            while (true) {
                val msg = session.incoming.receive() as Frame.Text
                radius.set(Random.int(50, 200))
                println(msg.readText())
            }
        }

        extend {
            drawer.fill = ColorRGBa.PINK
            drawer.circle(
                Polar(seconds * 50, 200.0).cartesian + drawer.bounds.center, radius.toDouble()
            )
        }
    }
}

```

---

_[View the full topic](https://openrndr.discourse.group/t/using-openrndr-as-a-websocket-client/532)._
