The endpoint sends Server Sent Events, as strings and it is reactive(Same as Firebase). Each record corresponds to one line(or one string), then I map it to the actual data class.
I want it to create me only one list so I can feed it to the ui as state, but somehow it creates a loop and keepts creating multiple lists as shown in the logs and also recomposes the ui in the same loop.
This is the SSE call done with Ktor Client.
fun getStream(): Flow<Note?> = callbackFlow {
try {
client.sse(
URL,
request = {
headers.append(HttpHeaders.Accept, "text/event-stream")
}, deserialize = { typeInfo, jsonString ->
val serializer = Json.serializersModule.serializer(typeInfo.kotlinType!!)
Json.decodeFromString(serializer, jsonString)!!
}) {
incoming.collect { event: TypedServerSentEvent<String> ->
val note: Note? = deserialize<Note>(event.data)
trySend(note)
}
}
} catch (e: Exception) {
e.printStackTrace()
}
}
This is how react to it in the viewmodel
class MainViewModel: ViewModel() {
private val ktorNetwork = KtorNetwork()
private val _uiState = MutableStateFlow<List<Note?>>(emptyList())
val uiState = _uiState.asStateFlow()
init {
getStream()
}
private fun getStream(){
val list = mutableListOf<Note>()
viewModelScope.launch {
ktorNetwork.getStream()
.distinctUntilChanged()
.collect { note ->
_uiState.update { currentList ->
currentList + note
}
}
}
}
UI:
@Composable
@Preview
fun App(
viewModel: MainViewModel = MainViewModel()
) {
val uiState = viewModel.uiState.collectAsState().value
MaterialTheme {
Column(
modifier = Modifier
.safeContentPadding()
.fillMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally,
) {
LazyColumn(
) {
items(
items = uiState,
key = { note -> note?.id ?: 0 }
){
if (it != null) {
Text(
modifier = Modifier.fillMaxWidth(),
text = it.title
)
}
}
}
}
}
}
And it keeps rebuilding the list and recomposing the UI nonstop.
2025-06-15 13:32:59.543 9835-9835 System.out com.mariomanhique.serversentevents I List of notes: [Note(id=1, title=Note1, content=My 1 ever note)]
2025-06-15 13:32:59.543 9835-9835 System.out com.mariomanhique.serversentevents I Note: Note(id=2, title=Note2, content=My 2 ever note)
2025-06-15 13:32:59.543 9835-9835 System.out com.mariomanhique.serversentevents I List of notes: [Note(id=1, title=Note1, content=My 1 ever note), Note(id=2, title=Note2, content=My 2 ever note)]
2025-06-15 13:32:59.607 9835-9835 System.out com.mariomanhique.serversentevents I Note: Note(id=3, title=Note3, content=My 3 ever note)
2025-06-15 13:32:59.607 9835-9835 System.out com.mariomanhique.serversentevents I List of notes: [Note(id=1, title=Note1, content=My 1 ever note), Note(id=2, title=Note2, content=My 2 ever note), Note(id=3, title=Note3, content=My 3 ever note)]
2025-06-15 13:32:59.607 9835-9835 System.out com.mariomanhique.serversentevents I Note: Note(id=4, title=Note4, content=My 4 ever note)
2025-06-15 13:32:59.607 9835-9835 System.out com.mariomanhique.serversentevents I List of notes: [Note(id=1, title=Note1, content=My 1 ever note), Note(id=2, title=Note2, content=My 2 ever note), Note(id=3, title=Note3, content=My 3 ever note), Note(id=4, title=Note4, content=My 4 ever note)]
2025-06-15 13:32:59.607 9835-9835 System.out com.mariomanhique.serversentevents I Note: Note(id=5, title=Note5, content=My 5 ever note)
2025-06-15 13:32:59.607 9835-9835 System.out com.mariomanhique.serversentevents I List of notes: [Note(id=1, title=Note1, content=My 1 ever note), Note(id=2, title=Note2, content=My 2 ever note), Note(id=3, title=Note3, content=My 3 ever note), Note(id=4, title=Note4, content=My 4 ever note), Note(id=5, title=Note5, content=My 5 ever note)]
2025-06-15 13:32:59.608 9835-9835 System.out com.mariomanhique.serversentevents I Note: Note(id=6, title=Note6, content=My 6 ever note)
2025-06-15 13:32:59.608 9835-9835 System.out com.mariomanhique.serversentevents I List of notes: [Note(id=1, title=Note1, content=My 1 ever note), Note(id=2, title=Note2, content=My 2 ever note), Note(id=3, title=Note3, content=My 3 ever note), Note(id=4, title=Note4, content=My 4 ever note), Note(id=5, title=Note5, content=My 5 ever note), Note(id=6, title=Note6, content=My 6 ever note)]
2025-06-15 13:32:59.608 9835-9835 System.out com.mariomanhique.serversentevents I Note: Note(id=7, title=Note7, content=My 7 ever note)
2025-06-15 13:32:59.608 9835-9835 System.out com.mariomanhique.serversentevents I List of notes: [Note(id=1, title=Note1, content=My 1 ever note), Note(id=2, title=Note2, content=My 2 ever note), Note(id=3, title=Note3, content=My 3 ever note), Note(id=4, title=Note4, content=My 4 ever note), Note(id=5, title=Note5, content=My 5 ever note), Note(id=6, title=Note6, content=My 6 ever note), Note(id=7, title=Note7, content=My 7 ever note)]
2025-06-15 13:32:59.609 9835-9854 EGL_emulation com.mariomanhique.serversentevents D app_time_stats: avg=90.70ms min=3.32ms max=324.32ms count=12
2025-06-15 13:32:59.669 9835-9835 System.out com.mariomanhique.serversentevents I Note: Note(id=8, title=Note8, content=My 8 ever note)
2025-06-15 13:32:59.669 9835-9835 System.out com.mariomanhique.serversentevents I List of notes: [Note(id=1, title=Note1, content=My 1 ever note), Note(id=2, title=Note2, content=My 2 ever note), Note(id=3, title=Note3, content=My 3 ever note), Note(id=4, title=Note4, content=My 4 ever note), Note(id=5, title=Note5, content=My 5 ever note), Note(id=6, title=Note6, content=My 6 ever note), Note(id=7, title=Note7, content=My 7 ever note), Note(id=8, title=Note8, content=My 8 ever note)]
2025-06-15 13:32:59.670 9835-9835 System.out com.mariomanhique.serversentevents I Note: Note(id=9, title=Note9, content=My 9 ever note)
2025-06-15 13:32:59.670 9835-9835 System.out com.mariomanhique.serversentevents I List of notes: [Note(id=1, title=Note1, content=My 1 ever note), Note(id=2, title=Note2, content=My 2 ever note), Note(id=3, title=Note3, content=My 3 ever note), Note(id=4, title=Note4, content=My 4 ever note), Note(id=5, title=Note5, content=My 5 ever note), Note(id=6, title=Note6, content=My 6 ever note), Note(id=7, title=Note7, content=My 7 ever note), Note(id=8, title=Note8, content=My 8 ever note), Note(id=9, title=Note9, content=My 9 ever note)]
2025-06-15 13:32:59.670 9835-9835 System.out com.mariomanhique.serversentevents I Note: Note(id=10, title=Note10, content=My 10 ever note)
2025-06-15 13:32:59.670 9835-9835 System.out com.mariomanhique.serversentevents I List of notes: [Note(id=1, title=Note1, content=My 1 ever note), Note(id=2, title=Note2, content=My 2 ever note), Note(id=3, title=Note3, content=My 3 ever note), Note(id=4, title=Note4, content=My 4 ever note), Note(id=5, title=Note5, content=My 5 ever note), Note(id=6, title=Note6, content=My 6 ever note), Note(id=7, title=Note7, content=My 7 ever note), Note(id=8, title=Note8, content=My 8 ever note), Note(id=9, title=Note9, content=My 9 ever note), Note(id=10, title=Note10, content=My 10 ever note)]
2025-06-15 13:32:59.672 9835-9835 System.out com.mariomanhique.serversentevents I Note: Note(id=11, title=Note11, content=My 11 ever note)
2025-06-15 13:32:59.672 9835-9835 System.out com.mariomanhique.serversentevents I List of notes: [Note(id=1, title=Note1, content=My 1 ever note), Note(id=2, title=Note2, content=My 2 ever note), Note(id=3, title=Note3, content=My 3 ever note), Note(id=4, title=Note4, content=My 4 ever note), Note(id=5, title=Note5, content=My 5 ever note), Note(id=6, title=Note6, content=My 6 ever note), Note(id=7, title=Note7, content=My 7 ever note), Note(id=8, title=Note8, content=My 8 ever note), Note(id=9, title=Note9, content=My 9 ever note), Note(id=10, title=Note10, content=My 10 ever note), Note(id=11, title=Note11, content=My 11 ever note)]
2025-06-15 13:32:59.672 9835-9835 System.out com.mariomanhique.serversentevents I Note: Note(id=12, title=Note12, content=My 12 ever note)
2025-06-15 13:32:59.672 9835-9835 System.out com.mariomanhique.serversentevents I List of notes: [Note(id=1, title=Note1, content=My 1 ever note), Note(id=2, title=Note2, content=My 2 ever note), Note(id=3, title=Note3, content=My 3 ever note), Note(id=4, title=Note4, content=My 4 ever note), Note(id=5, title=Note5, content=My 5 ever note), Note(id=6, title=Note6, content=My 6 ever note), Note(id=7, title=Note7, content=My 7 ever note), Note(id=8, title=Note8, content=My 8 ever note), Note(id=9, title=Note9, content=My 9 ever note), Note(id=10, title=Note10, content=My 10 ever note), Note(id=11, title=Note11, content=My 11 ever note), Note(id=12, title=Note12, content=My 12 ever note)]
2025-06-15 13:32:59.673 9835-9835 System.out com.mariomanhique.serversentevents I Note: Note(id=13, title=Note13, content=My 13 ever note)
2025-06-15 13:32:59.673 9835-9835 System.out com.mariomanhique.serversentevents I List of notes: [Note(id=1, title=Note1, content=My 1 ever note), Note(id=2, title=Note2, content=My 2 ever note), Note(id=3, title=Note3, content=My 3 ever note), Note(id=4, title=Note4, content=My 4 ever note), Note(id=5, title=Note5, content=My 5 ever note), Note(id=6, title=Note6, content=My 6 ever note), Note(id=7, title=Note7, content=My 7 ever note), Note(id=8, title=Note8, content=My 8 ever note), Note(id=9, title=Note9, content=My 9 ever note), Note(id=10, title=Note10, content=My 10 ever note), Note(id=11, title=Note11, content=My 11 ever note), Note(id=12, title=Note12, content=My 12 ever note), Note(id=13, title=Note13, content=My 13 ever note)]
2025-06-15 13:32:59.781 9835-9835 System.out com.mariomanhique.serversentevents I Note: Note(id=1, title=Note1, content=My 1 ever note)
2025-06-15 13:32:59.782 9835-9835 System.out com.mariomanhique.serversentevents I List of notes: [Note(id=1, title=Note1, content=My 1 ever note)]
2025-06-15 13:32:59.782 9835-9835 System.out com.mariomanhique.serversentevents I Note: Note(id=2, title=Note2, content=My 2 ever note)
2025-06-15 13:32:59.782 9835-9835 System.out com.mariomanhique.serversentevents I List of notes: [Note(id=1, title=Note1, content=My 1 ever note), Note(id=2, title=Note2, content=My 2 ever note)]
2025-06-15 13:32:59.783 9835-9835 System.out com.mariomanhique.serversentevents I Note: Note(id=3, title=Note3, content=My 3 ever note)
2025-06-15 13:32:59.783 9835-9835 System.out com.mariomanhique.serversentevents I List of notes: [Note(id=1, title=Note1, content=My 1 ever note), Note(id=2, title=Note2, content=My 2 ever note), Note(id=3, title=Note3, content=My 3 ever note)]
2025-06-15 13:32:59.783 9835-9835 System.out com.mariomanhique.serversentevents I Note: Note(id=4, title=Note4, content=My 4 ever note)
2025-06-15 13:32:59.783 9835-9835 System.out com.mariomanhique.serversentevents I List of notes: [Note(id=1, title=Note1, content=My 1 ever note), Note(id=2, title=Note2, content=My 2 ever note), Note(id=3, title=Note3, content=My 3 ever note), Note(id=4, title=Note4, content=My 4 ever note)]
2025-06-15 13:32:59.783 9835-9835 System.out com.mariomanhique.serversentevents I Note: Note(id=5, title=Note5, content=My 5 ever note)
2025-06-15 13:32:59.783 9835-9835 System.out com.mariomanhique.serversentevents I List of notes: [Note(id=1, title=Note1, content=My 1 ever note), Note(id=2, title=Note2, content=My 2 ever note), Note(id=3, title=Note3, content=My 3 ever note), Note(id=4, title=Note4, content=My 4 ever note), Note(id=5, title=Note5, content=My 5 ever note)]
2025-06-15 13:32:59.783 9835-9835 System.out com.mariomanhique.serversentevents I Note: Note(id=6, title=Note6, content=My 6 ever note)
2025-06-15 13:32:59.783 9835-9835 System.out com.mariomanhique.serversentevents I List of notes: [Note(id=1, title=Note1, content=My 1 ever note), Note(id=2, title=Note2, content=My 2 ever note), Note(id=3, title=Note3, content=My 3 ever note), Note(id=4, title=Note4, content=My 4 ever note), Note(id=5, title=Note5, content=My 5 ever note), Note(id=6, title=Note6, content=My 6 ever note)]
2025-06-15 13:32:59.783 9835-9835 System.out com.mariomanhique.serversentevents I Note: Note(id=7, title=Note7, content=My 7 ever note)
2025-06-15 13:32:59.783 9835-9835 System.out com.mariomanhique.serversentevents I List of notes: [Note(id=1, title=Note1, content=My 1 ever note), Note(id=2, title=Note2, content=My 2 ever note), Note(id=3, title=Note3, content=My 3 ever note), Note(id=4, title=Note4, content=My 4 ever note), Note(id=5, title=Note5, content=My 5 ever note), Note(id=6, title=Note6, content=My 6 ever note), Note(id=7, title=Note7, content=My 7 ever note)]
2025-06-15 13:32:59.783 9835-9835 System.out com.mariomanhique.serversentevents I Note: Note(id=8, title=Note8, content=My 8 ever note)
2025-06-15 13:32:59.783 9835-9835 System.out com.mariomanhique.serversentevents I List of notes: [Note(id=1, title=Note1, content=My 1 ever note), Note(id=2, title=Note2, content=My 2 ever note), Note(id=3, title=Note3, content=My 3 ever note), Note(id=4, title=Note4, content=My 4 ever note), Note(id=5, title=Note5, content=My 5 ever note), Note(id=6, title=Note6, content=My 6 ever note), Note(id=7, title=Note7, content=My 7 ever note), Note(id=8, title=Note8, content=My 8 ever note)]
2025-06-15 13:32:59.783 9835-9835 System.out com.mariomanhique.serversentevents I Note: Note(id=9, title=Note9, content=My 9 ever note)
2025-06-15 13:32:59.783 9835-9835 System.out com.mariomanhique.serversentevents I List of notes: [Note(id=1, title=Note1, content=My 1 ever note), Note(id=2, title=Note2, content=My 2 ever note), Note(id=3, title=Note3, content=My 3 ever note), Note(id=4, title=Note4, content=My 4 ever note), Note(id=5, title=Note5, content=My 5 ever note), Note(id=6, title=Note6, content=My 6 ever note), Note(id=7, title=Note7, content=My 7 ever note), Note(id=8, title=Note8, content=My 8 ever note), Note(id=9, title=Note9, content=My 9 ever note)]
2025-06-15 13:32:59.783 9835-9835 System.out com.mariomanhique.serversentevents I Note: Note(id=10, title=Note10, content=My 10 ever note)
2025-06-15 13:32:59.783 9835-9835 System.out com.mariomanhique.serversentevents I List of notes: [Note(id=1, title=Note1, content=My 1 ever note), Note(id=2, title=Note2, content=My 2 ever note), Note(id=3, title=Note3, content=My 3 ever note), Note(id=4, title=Note4, content=My 4 ever note), Note(id=5, title=Note5, content=My 5 ever note), Note(id=6, title=Note6, content=My 6 ever note), Note(id=7, title=Note7, content=My 7 ever note), Note(id=8, title=Note8, content=My 8 ever note), Note(id=9, title=Note9, content=My 9 ever note), Note(id=10, title=Note10, content=My 10 ever note)]
2025-06-15 13:32:59.784 9835-9835 System.out com.mariomanhique.serversentevents I Note: Note(id=11, title=Note11, content=My 11 ever note)
2025-06-15 13:32:59.784 9835-9835 System.out com.mariomanhique.serversentevents I List of notes: [Note(id=1, title=Note1, content=My 1 ever note), Note(id=2, title=Note2, content=My 2 ever note), Note(id=3, title=Note3, content=My 3 ever note), Note(id=4, title=Note4, content=My 4 ever note), Note(id=5, title=Note5, content=My 5 ever note), Note(id=6, title=Note6, content=My 6 ever note), Note(id=7, title=Note7, content=My 7 ever note), Note(id=8, title=Note8, content=My 8 ever note), Note(id=9, title=Note9, content=My 9 ever note), Note(id=10, title=Note10, content=My 10 ever note), Note(id=11, title=Note11, content=My 11 ever note)]
2025-06-15 13:32:59.784 9835-9835 System.out com.mariomanhique.serversentevents I Note: Note(id=12, title=Note12, content=My 12 ever note)
2025-06-15 13:32:59.784 9835-9835 System.out com.mariomanhique.serversentevents I List of notes: [Note(id=1, title=Note1, content=My 1 ever note), Note(id=2, title=Note2, content=My 2 ever note), Note(id=3, title=Note3, content=My 3 ever note), Note(id=4, title=Note4, content=My 4 ever note), Note(id=5, title=Note5, content=My 5 ever note), Note(id=6, title=Note6, content=My 6 ever note), Note(id=7, title=Note7, content=My 7 ever note), Note(id=8, title=Note8, content=My 8 ever note), Note(id=9, title=Note9, content=My 9 ever note), Note(id=10, title=Note10, content=My 10 ever note), Note(id=11, title=Note11, content=My 11 ever note), Note(id=12, title=Note12, content=My 12 ever note)]
2025-06-15 13:32:59.785 9835-9835 System.out com.mariomanhique.serversentevents I Note: Note(id=13, title=Note13, content=My 13 ever note)
2025-06-15 13:32:59.785 9835-9835 System.out com.mariomanhique.serversentevents I List of notes: [Note(id=1, title=Note1, content=My 1 ever note), Note(id=2, title=Note2, content=My 2 ever note), Note(id=3, title=Note3, content=My 3 ever note), Note(id=4, title=Note4, content=My 4 ever note), Note(id=5, title=Note5, content=My 5 ever note), Note(id=6, title=Note6, content=My 6 ever note), Note(id=7, title=Note7, content=My 7 ever note), Note(id=8, title=Note8, content=My 8 ever note), Note(id=9, title=Note9, content=My 9 ever note), Note(id=10, title=Note10, content=My 10 ever note), Note(id=11, title=Note11, content=My 11 ever note), Note(id=12, title=Note12, content=My 12 ever note), Note(id=13, title=Note13, content=My 13 ever note)]