Fix messages appearing out of order when local time is not synced with server time#6193
Draft
VelikovPetar wants to merge 1 commit intodevelopfrom
Draft
Fix messages appearing out of order when local time is not synced with server time#6193VelikovPetar wants to merge 1 commit intodevelopfrom
VelikovPetar wants to merge 1 commit intodevelopfrom
Conversation
…h server time Co-Authored-By: Claude <noreply@anthropic.com>
Contributor
PR checklist ❌The following issues were detected:
What we check
|
Contributor
SDK Size Comparison 📏
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Goal
Fix messages appearing out of order when a user's device clock is not in sync with the server, or when multiple messages are sent in rapid succession and their server-assigned timestamps don't preserve the local send order.
Implementation
Introduced
MessageSortComparator— a dedicatedComparator<Message>that uses a two-tier sorting strategy:createdLocallyAt: sort bycreatedLocallyAtto preserve the exact order the messages were composed, regardless of server-side timestamp assignment or network latency.createdAt, falling back tocreatedLocallyAtwhen the server timestamp is not yet available. This handles cross-user messages and ensures clock-skewed messages land in the correct position on the timeline.Replaced all existing
.sortedBy { it.getCreatedAtOrNull() }call-sites inChannelMutableState,ThreadMutableState,Channel.kt(extensions), andThread.kt(extensions) with.sortedWith(MessageSortComparator).Why sorting by
createdLocallyAtis reliable for same-user messages:ChatClient.sendMessagecallsMessage.ensureCreatedLocallyAt()before dispatching each message. That function setscreatedLocallyAttomax(channel.lastMessageAt + 1ms, now()), which guarantees the timestamp is always strictly after the previous message in the channel. As a result, successive messages from the same user always have monotonically increasingcreatedLocallyAtvalues that reflect the exact order they were composed — making it safe to use as the sort key for same-user pairs.UI Changes
Testing