https://kotlinlang.org logo
#ktor
Title
# ktor
d

Dariusz Kuc

06/25/2020, 1:14 AM
hello! I started looking into using websocket connections for streaming data and was wondering if anyone knows of some good guides around it, e.g. I’m trying to implement https://github.com/apollographql/subscriptions-transport-ws/blob/master/PROTOCOL.md so something akin to the following pseudocode
Copy code
<http://client.ws|client.ws>() {
  send(GQL_CONNECTION_INIT)
  val ackFlow = incoming.receiveAsFlow()
  // verify ack messages

  send(GQL_START)
  val response = incoming.receiveAsFlow() // <- HOW TO RETURN THIS?
  
  // error handling, closing session and unsubscribing omitted for clarity
}
I was hoping to wrap the above in some method that returns the
response: Flow
is that even possible? Further caveat is that this flow of responses would also have to be filtered for keep alive messages….
j

Joe

06/25/2020, 3:40 PM
i might do something like this pseudo code:
Copy code
val channel = Channel<Message>()
<http://client.ws|client.ws>() {
    send(GQL_CONNECTION_INIT)
    val ack = incoming.receive()
    // verify ack message
    send(GQL_START)
    for (frame in incoming) {
        val msg = Message(frame)
        if (msg != GQL_KA) { 
            channel.send(msg)
        }
     }
}
return channel.consumeAsFlow()
you can make things more complicated by allowing a single client/connection to handle multiple concurrent queries, too, but a single-query-per-connection could work something like the above, and be extended further in the future to handle the connection orthogonal to a query. something to think about when designing the interface at least.
d

Dariusz Kuc

06/25/2020, 3:41 PM
nice, thanks!
5 Views