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

David Nault

10/18/2021, 6:23 PM
Any tips or best practices for handling unsolicited responses when using raw sockets in a client? In other words, ktor makes it really easy to have a single coroutine send a request and read the response, but how should the code / coroutines be structured to allow for the server sending "responses" that don't have a corresponding client request? I'm assuming the client will need to read and write in separate coroutines and somehow correlate the requests and responses, but I'm wondering if there's a cleaner solution.
d

darkmoon_uk

10/19/2021, 12:15 AM
I appreciate this is a bit of a 'dodge' to your question, and there may be some more direct strategies about positively identifying a valid response... but I would also say, at some point, you would just have to consider unsolicited responses as protocol-breaking and entirely in error.
In other words, if your server is sending unsolicited responses you have 'bigger problems' than just identifying and rejecting them.
Beyond that and you start getting into protocol design; if you're looking to match requests and responses that arrive over the wire, you could have something as simple as a rolling number to identify the request/response. This may be useful if you're sending back asynchronous responses out-of-order, over the single channel.
That would let you have e.g. INT_MAX number of requests in-flight at any one time - should be enough I'd have thought!
👍 1
😆 1
d

David Nault

10/19/2021, 12:24 AM
Thanks Chris. In this scenario the "unsolicited" responses are part of the protocol, and are useful to the client... it's just that the client never knows when they might arrive. You correctly inferred that the protocol has an ID for matching a response with a request. My question is more along the lines of "I'm completely new to Ktor -- is there an easy recipe for doing this?" 🙂
d

darkmoon_uk

10/19/2021, 12:28 AM
I don't know that it has explicit support for this pattern per-se
But there has been one recent development in kotlinx-serialization land that may be of interest to you
That is support for stream-based (de)serialization.
Whereas before the library operated only with whole strings.
You can now push/pull any number of fully formed objects to/from a stream; haven't looked closely enough to see if this support extends to all formats or just JSON.
Sounds like you might be working with your own binary format however.
☝️ 1
Ktor also has a websocket w/ 'frames' abstraction in case that helps 🤷
I think Ktor isn't really a protocol toolkit as such; leaving you to either use a supported Protocol, or build your own from bits & bytes 🙂
d

David Nault

10/19/2021, 1:24 AM
Thanks, I'll look to the websocket implementation for inspiration.
4 Views