diff --git a/apps/meteor/app/lib/client/methods/sendMessage.ts b/apps/meteor/app/lib/client/methods/sendMessage.ts index 0969ea8d9cf36..ee67688a0b6ae 100644 --- a/apps/meteor/app/lib/client/methods/sendMessage.ts +++ b/apps/meteor/app/lib/client/methods/sendMessage.ts @@ -44,7 +44,13 @@ Meteor.methods({ await onClientMessageReceived(message as IMessage).then((message) => { Messages.state.store(message); - return clientCallbacks.run('afterSaveMessage', message, { room, user }); + void clientCallbacks.run('afterSaveMessage', message, { room, user }); + + // Now that the message is stored, we can go ahead and mark as sent + Messages.state.update( + (record) => record._id === message._id && record.temp === true, + ({ temp: _, ...record }) => record, + ); }); }, }); diff --git a/apps/meteor/app/ui-utils/client/lib/LegacyRoomManager.ts b/apps/meteor/app/ui-utils/client/lib/LegacyRoomManager.ts index d6a8abf9c35da..0c1513e42fbe1 100644 --- a/apps/meteor/app/ui-utils/client/lib/LegacyRoomManager.ts +++ b/apps/meteor/app/ui-utils/client/lib/LegacyRoomManager.ts @@ -177,7 +177,17 @@ const openRoom = (typeName: string, record: OpenedRoom) => { if (msg.t !== 'command') { const subscription = Subscriptions.state.find(({ rid }) => rid === record.rid); const isNew = !Messages.state.find((record) => record._id === msg._id && record.temp !== true); - ({ _id: msg._id, temp: { $ne: true } }); + + // Measure and log message receive delay for messages + if (msg.ts) { + const receiveDelay = Date.now() - new Date(msg.ts).getTime(); + + // Log warning if delay is significant (>2 seconds) + if (receiveDelay > 2000) { + console.warn(`[Message Delivery] High delay detected: ${receiveDelay}ms. Possible network or backend issue.`); + } + } + await upsertMessage({ msg, subscription }); if (isNew) { await clientCallbacks.run('streamNewMessage', msg); diff --git a/apps/meteor/client/stores/Messages.ts b/apps/meteor/client/stores/Messages.ts index ff9fcdeee9f38..09706de1d95b2 100644 --- a/apps/meteor/client/stores/Messages.ts +++ b/apps/meteor/client/stores/Messages.ts @@ -6,5 +6,12 @@ import { createGlobalStore } from '../lib/cachedStores/createGlobalStore'; /** @deprecated prefer fetching data from the REST API, listening to changes via streamer events, and storing the state in a Tanstack Query */ export const Messages = createGlobalStore( - createDocumentMapStore(), + createDocumentMapStore< + IMessage & { + ignored?: boolean; + autoTranslateFetching?: boolean; + autoTranslateShowInverse?: boolean; + temp?: boolean; + } + >(), );