I'm trying to create a simple Chat app. Is there ...
# multiplatform
d
I'm trying to create a simple Chat app. Is there any guide to managing a WebSocket session in compose?
d
You might look at
<https://github.com/Kotlin/kotlinx-rpc>
for this; you can think of it as 'retrofit, for WebSockets' in the way that it makes them easier to use.
1
d
I've got the client/server protocol already built, I'm more curious about how best to manage the http client and web socket session in the context of Composables.
a
I'd want to decouple the UI from the server chat part of the app...your UI should just be updated with immutable state and know nothing about the underlying how that state was generated.
👆 3
p
I would have the SocketConnectionsManager/ChatManager living in the global state. Then get it injected in ViewModels when needed. For instance, in a ChatScreenComposable that uses a ChatScreenViewModel. Keep in mind WebSockets have a limited time alive in iOS and Android. So rely on a strong polling mechanism based on FCM(Firebase Cloud Messaging) notifications
1
d
Agree, the straightforward answer is... The WebSocket management and Composables are separate things. Clean architecture promotes this idea, where Composables reside in your presentation layer and WebSocket in your service layer, decoupled via Domain. In this case the notional 'Domain' might be as thin as some DI that installs your WebSocket service in a ViewModel, but unless you really want WebSocket scope hard-tied to a View lifecycle (which sounds unusual) then you'd typically want to derive a Coroutines Scope from the root/Global.
d
Thanks. I haven’t really done app development before, but I’ve done desktop and web ui (the old fashioned way). I wasn’t really thinking of the app as something distinct from the Compose tree, but I see now that I’ve really limited myself by doing that. I haven’t used ViewModel yet. Where can I learn more about how and why it’s used?
d
Big topic; Google and AI.
d
Sure thing. Thanks.
r
The #C03GTEJ9Y3E docs actually have a pretty good introduction to the various UI programming architectures: https://copper-leaf.github.io/ballast/wiki/usage/mental-model/
t
Ktor is good. I also have a websocket library to handle this. https://github.com/TheArchitect123/TitanSocket
d
Yep, I'm using Ktor. My question was more about lifecycle of the operations. ViewModels was the missing puzzle piece for me.