Anyone here using Jetpack Compose? I'm wondering w...
# android
a
Anyone here using Jetpack Compose? I'm wondering what the best way to stream messages from a web socket and have them display in composable - while using "idiomatic" Kotlin (i.e. coroutines). There seems to be not too many examples of web sockets with coroutines online, and no examples with jetpack compose. So it would be great if anyone had any suggestions at things to look at 🙂
a
first resource, the #compose channel 🙂 we're building quite a community in there!
🙌 2
second, when implementing things like what you're describing, websocket or any other event/message stream, you'll have some step in your pipeline that updates state that your compose code will consume
as a very rough example, if you were receiving log messages from your websocket, you might have a coroutine reading those messages from the socket via a channel or flow or similar, and appending each one to a list that represents your full log
concretely, that list might be created using
mutableStateListOf
, which gives you a list that will automatically invalidate any composable that consumed it whenever it changes
a
@Adam Powell Thanks for the channel link! I've been using compose to prototype my project, and it's been pretty scary how fast I've been going. Yeah I was thinking about using a mutable list, from further research it looks like Tinder's Scarlet library can using Kotlin coroutines so I'll have a go at using them for this 🤞
👍 2
k
Scarlet is not being actively maintained, there are many issues on the repo which are unaswered so I end up writing a
callbackFlow
for okhttp's Websocket where I used
connectionStatus: MutableStateFlow<ConnectionStatus>
(a simple enum representing several states) and
messages: MutableSharedFlow<String>
to maintain a flow of incoming messages. Both of these flows were being populated via a
callbackFlow
2
🙌 1
u
#compose