Skip to content

Comments

[DX-475] Updated docs section for handling self published message#3215

Open
sacOO7 wants to merge 1 commit intomainfrom
fix/chat-self-publish-message
Open

[DX-475] Updated docs section for handling self published message#3215
sacOO7 wants to merge 1 commit intomainfrom
fix/chat-self-publish-message

Conversation

@sacOO7
Copy link
Contributor

@sacOO7 sacOO7 commented Feb 19, 2026

Summary by CodeRabbit

Documentation

  • Added a new section on handling self-published messages in chat, including multiple deduplication approaches with code examples in JavaScript, React, Swift, and Kotlin, plus best practices for implementing optimistic UI updates.

@coderabbitai
Copy link

coderabbitai bot commented Feb 19, 2026

Walkthrough

Documentation addition to chat messages page that introduces two approaches for handling self-published messages in chat rooms: subscription-only rendering and serial field-based deduplication. Includes multi-language code examples (JavaScript, React, Swift, Kotlin, Android) demonstrating both strategies with optimistic UI patterns.

Changes

Cohort / File(s) Summary
Chat Messages Documentation
src/pages/docs/chat/rooms/messages.mdx
Adds new section on self-published message handling with two deduplication strategies: (1) rendering exclusively from subscription flow, (2) checking message.serial field before appending. Includes code examples across five languages and demonstrates optimistic update patterns.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 A message bounced back, oh what a sight,
Duplicates dancing, not quite right!
But now we guide with wisdom clear,
Two paths to peace, both drawing near—
Serial checks and subscribed grace,
Keep your UI in the perfect place! 📬✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly references the issue DX-475 and accurately summarizes the main change: updating documentation for handling self-published messages.
Linked Issues check ✅ Passed The PR documentation addition addresses all primary objectives from DX-475: documenting self-published message echoing behavior, explaining duplicate UI issues, providing clear filtering guidance with examples in multiple languages (JavaScript, React, Swift, Kotlin, Android), and demonstrating production-ready approaches using serial field deduplication and optimistic UI patterns.
Out of Scope Changes check ✅ Passed All changes are directly scoped to DX-475 requirements. The PR only adds documentation section covering self-published message handling with deduplication approaches, with no unrelated modifications or scope creep detected.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/chat-self-publish-message

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@sacOO7 sacOO7 changed the title [DX-475] Updated docs section for handling self published message to messages.kt [DX-475] Updated docs section for handling self published message Feb 19, 2026
@sacOO7
Copy link
Contributor Author

sacOO7 commented Feb 19, 2026

@coderabbitai review

@coderabbitai
Copy link

coderabbitai bot commented Feb 19, 2026

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@sacOO7 sacOO7 requested a review from Copilot February 19, 2026 08:37
@sacOO7 sacOO7 marked this pull request as ready for review February 19, 2026 08:42
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds documentation for handling self-published messages in chat applications to prevent message duplication. When a message is sent using send(), the server echoes it back to all subscribers including the sender. This can cause messages to appear twice in the UI if not handled properly.

Changes:

  • Added a new "Handle self-published messages" section with guidance on two approaches: only rendering from subscription (simple) or using serial-based deduplication for optimistic UI updates
  • Provided code examples in JavaScript, React, Swift, Kotlin, and Android demonstrating the serial-based deduplication approach
  • Included explanatory documentation about when to use each approach

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🧹 Nitpick comments (1)
src/pages/docs/chat/rooms/messages.mdx (1)

238-240: Optional: add a code example for Approach 1.

Approach 1 is described only in text. A minimal code snippet (subscribe-only rendering, no optimistic add) alongside Approach 2 would make the docs consistent and let developers copy-paste either pattern without having to infer what Approach 1 looks like in code.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/pages/docs/chat/rooms/messages.mdx` around lines 238 - 240, Add a minimal
code example to the "Approach 1" section showing the subscribe-only rendering
pattern: show a snippet that subscribes to message events (e.g., onMessage or
messagesSubscription handler), appends incoming messages to local state (e.g.,
setMessages or messagesRef), and calls send() without mutating the UI
optimistically; reference the same send() function and the subscription listener
used elsewhere in the doc so readers can copy-paste a simple pattern where
send() only sends to the server and the subscription callback is solely
responsible for adding messages to the local list.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/pages/docs/chat/rooms/messages.mdx`:
- Around line 233-241: Add a single clarifying sentence inside the existing
<Aside data-type='important'> block (near the explanation of send() and
subscribe()) stating that setting echoMessages=false does not disable the Chat
SDK's echo behavior—echoes are emitted by the Chat service itself and must be
deduplicated client-side (e.g., via serial or rendering only from
subscriptions). Reference the existing terms send(), subscribe(), serial, and
echoMessages=false so readers can immediately connect the caveat to the
surrounding guidance.
- Around line 297-314: The code mutates the shared var messages from two
concurrent contexts (the unstructured Task processing messagesSubscription and
the outer async scope) causing a data race; confine all mutations to the
MainActor by ensuring any append/contains on messages runs on the MainActor—wrap
the loop body inside the Task handling messagesSubscription so each iteration
does await MainActor.run { if !messages.contains(where: { $0.serial ==
message.serial }) { messages.append(message) } } and do the same for the
optimistic append (await MainActor.run { messages.append(sentMessage) }), or
alternatively annotate the surrounding UI/async scope (the function or enclosing
type that owns messages and invokes room.messages.send/write) with `@MainActor` so
both contexts perform mutations under actor isolation (reference: messages,
messagesSubscription, Task { ... }, and room.messages.send).
- Around line 262-263: The optimistic push after await room.messages.send(...)
is unconditional and can create duplicates; mirror the subscription's serial
check before adding the sent message: after calling room.messages.send(...) and
receiving message, verify the message.serial (or the same unique identifier used
by the subscription callback) is not already present in the messages collection
(e.g., check messages array for an existing entry with that serial) and only
call messages.push(message) when it's missing; apply this symmetric serial check
in the send-side code path that currently does messages.push(message).

---

Nitpick comments:
In `@src/pages/docs/chat/rooms/messages.mdx`:
- Around line 238-240: Add a minimal code example to the "Approach 1" section
showing the subscribe-only rendering pattern: show a snippet that subscribes to
message events (e.g., onMessage or messagesSubscription handler), appends
incoming messages to local state (e.g., setMessages or messagesRef), and calls
send() without mutating the UI optimistically; reference the same send()
function and the subscription listener used elsewhere in the doc so readers can
copy-paste a simple pattern where send() only sends to the server and the
subscription callback is solely responsible for adding messages to the local
list.

@sacOO7 sacOO7 force-pushed the fix/chat-self-publish-message branch from 4e30b7c to e1362a1 Compare February 19, 2026 14:29
@sacOO7
Copy link
Contributor Author

sacOO7 commented Feb 19, 2026

@coderabbitai review

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 6 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

<Aside data-type='important'>
When you send a message using `send()`, the server echoes it back to all subscribers in the room, including the sender.
If your application adds the message to the UI immediately after calling `send()` and also appends it when received via `subscribe()`, the message will appear twice.
To avoid this, we need to add safety check in the subscribe method that validates incoming message serial and version against existing messages.
Copy link

Copilot AI Feb 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing article "a" before "safety check". The sentence should read "we need to add a safety check in the subscribe method..."

Suggested change
To avoid this, we need to add safety check in the subscribe method that validates incoming message serial and version against existing messages.
To avoid this, we need to add a safety check in the subscribe method that validates incoming message serial and version against existing messages.

Copilot uses AI. Check for mistakes.
```

```kotlin
val myMessageList: List<Messages>
Copy link

Copilot AI Feb 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Type should be List<Message> (singular) instead of List<Messages> (plural). The Message type is singular throughout the codebase as shown in other examples like var myMessageList by remember { mutableStateOf<List<Message>>(emptyList()) } on line 301.

Suggested change
val myMessageList: List<Messages>
val myMessageList: List<Message>

Copilot uses AI. Check for mistakes.
@@ -448,11 +549,17 @@ for await message in messagesSubscription {
```kotlin
val myMessageList: List<Messages>
Copy link

Copilot AI Feb 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Type should be List<Message> (singular) instead of List<Messages> (plural). The Message type is singular throughout the codebase as shown in other examples.

Suggested change
val myMessageList: List<Messages>
val myMessageList: List<Message>

Copilot uses AI. Check for mistakes.
@@ -702,11 +825,17 @@ for await message in messagesSubscription {
```kotlin
val myMessageList: List<Messages>
Copy link

Copilot AI Feb 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Type should be List<Message> (singular) instead of List<Messages> (plural). The Message type is singular throughout the codebase as shown in other examples.

Suggested change
val myMessageList: List<Messages>
val myMessageList: List<Message>

Copilot uses AI. Check for mistakes.
@@ -431,12 +525,19 @@ const MyComponent = () => {
let messagesList: [Message]
Copy link

Copilot AI Feb 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The messagesList is declared as let (immutable) but the code attempts to modify it with append() and index assignment operations on lines 536 and 541. This will cause a compilation error. Change let to var to make the array mutable.

Suggested change
let messagesList: [Message]
var messagesList: [Message]

Copilot uses AI. Check for mistakes.
@@ -685,12 +801,19 @@ const MyComponent = () => {
let messagesList: [Message]
Copy link

Copilot AI Feb 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The messagesList is declared as let (immutable) but the code attempts to modify it with append() and remove() operations on lines 812 and 817. This will cause a compilation error. Change let to var to make the array mutable.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant