refactor: sync missed messages via REST chat.syncMessages on reconnect - #41189
refactor: sync missed messages via REST chat.syncMessages on reconnect#41189VanshajPoonia wants to merge 4 commits into
Conversation
Reconnect sync previously used the loadMissedMessages DDP method, which
queries by creation timestamp and cannot see edits or deletions that
happened while the client was offline. The client now calls
GET /v1/chat.syncMessages with the latest locally known _updatedAt as
baseline, upserts the returned updated messages and removes the deleted
ones from the message store.
Adds a { rid: 1, _updatedAt: 1 } index to the messages collection so the
sync query is bounded per room, since the existing { rid, ts, _updatedAt }
index cannot serve the _updatedAt range. The trash collection already has
{ rid, __collection__, _deletedAt } covering the deleted side.
The loadMissedMessages method is now deprecated, planned for removal in
9.0.0.
Closes RocketChat#41079
Closes RocketChat#41000
- Skip store records without _updatedAt when picking the sync baseline, so an optimistic message whose temp flag was already cleared (but which never gained an _updatedAt) can no longer crash the comparator - Wrap the whole sync body in the try/catch so a stray throw can't escape as an unhandled rejection from the unawaited call - Batch deletions through Messages.state.remove instead of a per-id delete loop, matching the deleteMessageBulk stream handler and notifying once - Use the exported EndpointFunction type and Object.values
|
Looks like this PR is not ready to merge, because of the following issues:
Please fix the issues and try again If you have any trouble, please check the PR guidelines |
🦋 Changeset detectedLatest commit: 68a0b9e The changes in this PR will be included in the next version bump. This PR includes changesets to release 4 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (3)
🚧 Files skipped from review as they are similar to previous changes (1)
📜 Recent review details⏰ Context from checks skipped due to timeout. (1)
🧰 Additional context used📓 Path-based instructions (2)**/*.{ts,tsx,js}📄 CodeRabbit inference engine (.cursor/rules/playwright.mdc)
Files:
**/*.spec.ts📄 CodeRabbit inference engine (.cursor/rules/playwright.mdc)
Files:
🧠 Learnings (5)📚 Learning: 2026-02-26T19:25:44.063ZApplied to files:
📚 Learning: 2026-02-26T19:25:44.063ZApplied to files:
📚 Learning: 2026-05-06T12:21:44.083ZApplied to files:
📚 Learning: 2026-02-24T19:22:48.358ZApplied to files:
📚 Learning: 2026-03-06T18:10:15.268ZApplied to files:
🔇 Additional comments (2)
WalkthroughThis PR moves reconnect message reconciliation to ChangesReconnect sync migration
Estimated code review effort: 4 (Complex) | ~45 minutes Possibly related issues
Possibly related PRs
Suggested labels: Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
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. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
packages/models/src/models/Messages.ts (1)
46-51: 🚀 Performance & Scalability | 🔵 TrivialSolid, well-documented index addition.
The
{ rid: 1, _updatedAt: 1 }index correctly targetsfindForUpdates's range query, and the comment clearly explains why the adjacent{ rid, ts, _updatedAt }index can't serve this pattern.One thing worth keeping in mind operationally: the downstream consumer (
handleWithoutPaginationinapps/meteor/server/publications/messages.ts, not in this diff) sorts the matched set bytswithout a limit. For clients that stay offline a long time in very active rooms, the matched-and-sorted set could grow large enough to add memory/sort overhead even with this index in place, sincetsisn't part of the new index. Not a regression from this PR, just a scaling consideration for the overall sync design.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/models/src/models/Messages.ts` around lines 46 - 51, The new { rid: 1, _updatedAt: 1 } index fixes the filter path, but `handleWithoutPagination` still materializes and sorts all matched messages by `ts`, which can become expensive for large offline syncs. Update the sync flow in `handleWithoutPagination`/`chat.syncMessages` to avoid a full in-memory sort of the entire result set, either by batching/paginating the fetch or by aligning the query and index strategy with the `ts` ordering. Keep the existing `modelIndexes()` change, but make the downstream consumer scale without relying on an unbounded sort.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@packages/models/src/models/Messages.ts`:
- Around line 46-51: The new { rid: 1, _updatedAt: 1 } index fixes the filter
path, but `handleWithoutPagination` still materializes and sorts all matched
messages by `ts`, which can become expensive for large offline syncs. Update the
sync flow in `handleWithoutPagination`/`chat.syncMessages` to avoid a full
in-memory sort of the entire result set, either by batching/paginating the fetch
or by aligning the query and index strategy with the `ts` ordering. Keep the
existing `modelIndexes()` change, but make the downstream consumer scale without
relying on an unbounded sort.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 74bb7365-2754-412b-b9a6-0c1212f07883
📒 Files selected for processing (6)
.changeset/reconnect-sync-rest-messages.mdapps/meteor/app/ui-utils/client/index.tsapps/meteor/client/views/root/hooks/useLoadMissedMessages.spec.tsapps/meteor/client/views/root/hooks/useLoadMissedMessages.tsapps/meteor/server/methods/loadMissedMessages.tspackages/models/src/models/Messages.ts
📜 Review details
⏰ Context from checks skipped due to timeout. (1)
- GitHub Check: cubic · AI code reviewer
🧰 Additional context used
📓 Path-based instructions (2)
**/*.{ts,tsx,js}
📄 CodeRabbit inference engine (.cursor/rules/playwright.mdc)
**/*.{ts,tsx,js}: Write concise, technical TypeScript/JavaScript with accurate typing in Playwright tests
Avoid code comments in the implementation
Files:
apps/meteor/app/ui-utils/client/index.tsapps/meteor/server/methods/loadMissedMessages.tspackages/models/src/models/Messages.tsapps/meteor/client/views/root/hooks/useLoadMissedMessages.tsapps/meteor/client/views/root/hooks/useLoadMissedMessages.spec.ts
**/*.spec.ts
📄 CodeRabbit inference engine (.cursor/rules/playwright.mdc)
**/*.spec.ts: Use descriptive test names that clearly communicate expected behavior in Playwright tests
Use.spec.tsextension for test files (e.g.,login.spec.ts)
Files:
apps/meteor/client/views/root/hooks/useLoadMissedMessages.spec.ts
🧠 Learnings (9)
📚 Learning: 2026-02-26T19:25:44.063Z
Learnt from: gabriellsh
Repo: RocketChat/Rocket.Chat PR: 38778
File: packages/ui-voip/src/providers/useMediaSession.ts:192-192
Timestamp: 2026-02-26T19:25:44.063Z
Learning: In the Rocket.Chat repository, do not reference Biome lint rules in code review feedback. Biome is not used even if biome.json exists; only reference Biome rules if there is explicit, project-wide usage documented. For TypeScript files, review lint implications without Biome guidance unless the project enables Biome rules.
Applied to files:
apps/meteor/app/ui-utils/client/index.tsapps/meteor/server/methods/loadMissedMessages.tspackages/models/src/models/Messages.tsapps/meteor/client/views/root/hooks/useLoadMissedMessages.tsapps/meteor/client/views/root/hooks/useLoadMissedMessages.spec.ts
📚 Learning: 2026-02-26T19:25:44.063Z
Learnt from: gabriellsh
Repo: RocketChat/Rocket.Chat PR: 38778
File: packages/ui-voip/src/providers/useMediaSession.ts:192-192
Timestamp: 2026-02-26T19:25:44.063Z
Learning: In this repository (RocketChat/Rocket.Chat), Biome lint rules are not used even if a biome.json exists. When reviewing TypeScript files (e.g., packages/ui-voip/src/providers/useMediaSession.ts), ensure lint suggestions do not reference Biome-specific rules. Rely on general ESLint/TypeScript lint rules and project conventions instead.
Applied to files:
apps/meteor/app/ui-utils/client/index.tsapps/meteor/server/methods/loadMissedMessages.tspackages/models/src/models/Messages.tsapps/meteor/client/views/root/hooks/useLoadMissedMessages.tsapps/meteor/client/views/root/hooks/useLoadMissedMessages.spec.ts
📚 Learning: 2026-05-06T12:21:44.083Z
Learnt from: juliajforesti
Repo: RocketChat/Rocket.Chat PR: 40256
File: apps/meteor/client/components/CreateDiscussion/CreateDiscussion.tsx:121-149
Timestamp: 2026-05-06T12:21:44.083Z
Learning: Field wrappers in rocket.chat/fuselage-forms (Field, FieldLabel, FieldRow, FieldError, FieldHint) auto-create htmlFor/id associations, aria-describedby, and role="alert" for errors. Do not manually set htmlFor, id, aria-describedby, or role attributes when using these wrappers. This automatic wiring does not apply to plain rocket.chat/fuselage components, which require explicit ID wiring per the accessibility docs. In code reviews, prefer using fuselage-forms wrappers for form fields and verify there is no unnecessary manual ID/aria wiring in files that use these wrappers. If a component uses plain fuselage components, ensure proper id wiring as per docs.
Applied to files:
apps/meteor/app/ui-utils/client/index.tsapps/meteor/server/methods/loadMissedMessages.tspackages/models/src/models/Messages.tsapps/meteor/client/views/root/hooks/useLoadMissedMessages.tsapps/meteor/client/views/root/hooks/useLoadMissedMessages.spec.ts
📚 Learning: 2026-03-16T21:50:37.589Z
Learnt from: amitb0ra
Repo: RocketChat/Rocket.Chat PR: 39676
File: .changeset/migrate-users-register-openapi.md:3-3
Timestamp: 2026-03-16T21:50:37.589Z
Learning: For changes related to OpenAPI migrations in Rocket.Chat/OpenAPI, when removing endpoint types and validators from rocket.chat/rest-typings (e.g., UserRegisterParamsPOST, /v1/users.register) document this as a minor changeset (not breaking) per RocketChat/Rocket.Chat-Open-API#150 Rule 7. Note that the endpoint type is re-exposed via a module augmentation .d.ts in the consuming package (e.g., packages/web-ui-registration/src/users-register.d.ts). In reviews, ensure the changeset clearly states: this is a non-breaking change, the major version should not be bumped, and the changeset reflects a minor version bump. Do not treat this as a breaking change during OpenAPI migrations.
Applied to files:
.changeset/reconnect-sync-rest-messages.md
📚 Learning: 2026-03-09T18:39:14.020Z
Learnt from: Harxhit
Repo: RocketChat/Rocket.Chat PR: 39476
File: apps/meteor/server/methods/addAllUserToRoom.ts:0-0
Timestamp: 2026-03-09T18:39:14.020Z
Learning: When implementing batch processing in server methods, favor a single data pass to collect both the items and any derived fields needed for validation. Use the same dataset for both validation and processing to avoid races between validation and execution, and document the approach in code comments. Apply this pattern to similar Meteor Rocket.Chat server methods under apps/meteor/server/methods to prevent race conditions and ensure consistent batch behavior.
Applied to files:
apps/meteor/server/methods/loadMissedMessages.ts
📚 Learning: 2026-02-10T16:32:42.586Z
Learnt from: tassoevan
Repo: RocketChat/Rocket.Chat PR: 38528
File: apps/meteor/client/startup/roles.ts:14-14
Timestamp: 2026-02-10T16:32:42.586Z
Learning: In Rocket.Chat's Meteor client code, DDP streams use EJSON and Date fields arrive as Date objects; do not manually construct new Date() in stream handlers (for example, in sdk.stream()). Only REST API responses return plain JSON where dates are strings, so implement explicit conversion there if needed. Apply this guidance to all TypeScript files under apps/meteor/client to ensure consistent date handling in DDP streams and REST responses.
Applied to files:
apps/meteor/client/views/root/hooks/useLoadMissedMessages.tsapps/meteor/client/views/root/hooks/useLoadMissedMessages.spec.ts
📚 Learning: 2026-05-11T20:30:35.265Z
Learnt from: tassoevan
Repo: RocketChat/Rocket.Chat PR: 40480
File: apps/meteor/client/meteor/startup/accounts.ts:59-61
Timestamp: 2026-05-11T20:30:35.265Z
Learning: In Rocket.Chat’s Meteor client code, when calling `dispatchToastMessage` with `{ type: 'error' }`, pass the raw caught error object as `message` without manual normalization. `dispatchToastMessage` is designed to accept `message: unknown` for error toasts, so avoid converting errors to strings (e.g., `String(error)`) or extracting `error.message` before passing them.
Applied to files:
apps/meteor/client/views/root/hooks/useLoadMissedMessages.tsapps/meteor/client/views/root/hooks/useLoadMissedMessages.spec.ts
📚 Learning: 2026-02-24T19:22:48.358Z
Learnt from: juliajforesti
Repo: RocketChat/Rocket.Chat PR: 38493
File: apps/meteor/tests/e2e/omnichannel/omnichannel-send-pdf-transcript.spec.ts:66-67
Timestamp: 2026-02-24T19:22:48.358Z
Learning: In Playwright end-to-end tests (e.g., under apps/meteor/tests/e2e/...), prefer locating elements by translated text (getByText) and ARIA roles (getByRole) over data-qa attributes. If translation values change, update the corresponding test locators accordingly. Never use data-qa locators. This guideline applies to all Playwright e2e test specs in the repository and helps keep tests robust to UI text changes and accessible semantics.
Applied to files:
apps/meteor/client/views/root/hooks/useLoadMissedMessages.spec.ts
📚 Learning: 2026-03-06T18:10:15.268Z
Learnt from: tassoevan
Repo: RocketChat/Rocket.Chat PR: 39397
File: packages/gazzodown/src/code/CodeBlock.spec.tsx:47-68
Timestamp: 2026-03-06T18:10:15.268Z
Learning: In tests (especially those using testing-library/dom/jsdom) for Rocket.Chat components, the HTML <code> element has an implicit ARIA role of 'code'. Therefore, screen.getByRole('code') or screen.findByRole('code') will locate <code> elements even without a role attribute. Do not flag findByRole('code') as invalid in reviews; prefer using the implicit role instead of adding role="code" unless necessary for accessibility.
Applied to files:
apps/meteor/client/views/root/hooks/useLoadMissedMessages.spec.ts
🔇 Additional comments (6)
apps/meteor/server/methods/loadMissedMessages.ts (1)
8-21: LGTM!.changeset/reconnect-sync-rest-messages.md (1)
1-7: LGTM!apps/meteor/app/ui-utils/client/index.ts (1)
1-3: LGTM!apps/meteor/client/views/root/hooks/useLoadMissedMessages.ts (2)
10-46: LGTM!
56-63: 📐 Maintainability & Code QualityNo change needed here.
syncMissedMessagesalready handles its own errors, and@typescript-eslint/no-floating-promisesis disabled forapps/meteor/client/**/*.ts.> Likely an incorrect or invalid review comment.apps/meteor/client/views/root/hooks/useLoadMissedMessages.spec.ts (1)
1-225: LGTM!
There was a problem hiding this comment.
1 issue found across 6 files
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
- Sort chat.syncMessages without-pagination results by _updatedAt instead
of ts so the new { rid, _updatedAt } index serves both the range filter
and the ordering, avoiding an in-memory sort on large offline syncs. This
matches the cursor-paginated path, which already orders by _updatedAt, and
keeps the ts-descending order for normal sequential sends. Unit test
assertions updated to match.
- Restore spies via afterEach in the useLoadMissedMessages spec so a failed
assertion can no longer leak the console.error mock into other tests.
Proposed changes (including videos or screenshots)
Migrates reconnect message synchronization from the
loadMissedMessagesDDP method to theGET /v1/chat.syncMessagesREST endpoint, so edits and deletions made while a client was offline are applied when the connection is restored.useLoadMissedMessagesnow sends the latest locally known_updatedAtaslastUpdate, maps the payload withmapMessageFromApi, upserts updated messages throughupsertMessageBulk(keeps the e2e pending flag handling) and removes deleted messages viaMessages.state.remove, mirroring the livedeleteMessageBulkstream handler_updatedAt) are skipped when choosing the sync baseline{ rid: 1, _updatedAt: 1 }index to the messages collection.findForUpdatesfilters byridplus an_updatedAtrange, which the existing{ rid: 1, ts: 1, _updatedAt: 1 }index cannot bound becausetssits between the two fields{ rid: 1, __collection__: 1, _deletedAt: 1 }already exists and serves the deleted side of the queryloadMissedMessagesmethod (removal planned for 9.0.0) instead of removing it, since external clients can still call it throughmethod.callIssue(s)
Closes #41079
Closes #41000
Steps to test or reproduce
Further comments
Kept a single
lastUpdaterequest per opened room instead of cursor pagination: only currently opened rooms are synced (capped bymaxRoomsOpen) and the endpoint already supportstype/nextcursors if bounded batches become necessary.Known follow-up, tracked in #41190: the open thread panel and infinite message lists (threads, discussions, omnichannel history) keep their own query caches fed only by live stream events, so they are not reconciled on a DDP-only reconnect. This is a pre-existing gap (the old DDP method also only wrote the message store) and is best solved across all those caches at once.
Lint, typecheck and the jest suite pass locally.
Summary by CodeRabbit
Summary by CodeRabbit
New Features
Bug Fixes
Chores