@ethora/chat-component 26.2.11 → 26.2.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -138,6 +138,34 @@ export default function App() {
138
138
  }
139
139
  ```
140
140
 
141
+ ### Single XMPP Initialization Contract
142
+
143
+ To avoid duplicated `wss://.../ws` connections, keep a single XMPP init source:
144
+
145
+ - `initBeforeLoad: true` -> `XmppProvider` is the only place that initializes XMPP.
146
+ - `initBeforeLoad: false` -> `Chat` (`useChatWrapperInit`) initializes XMPP.
147
+
148
+ If your app also has external `client.login(...)` logic, guard it:
149
+
150
+ ```ts
151
+ if (chatConfig.initBeforeLoad) {
152
+ console.warn('[XMPP] initBeforeLoad=true, skip external client.login()');
153
+ return;
154
+ }
155
+
156
+ await client.login(...);
157
+ ```
158
+
159
+ Also pass a memoized config object to both `XmppProvider` and `Chat`:
160
+
161
+ ```tsx
162
+ const chatConfig = useMemo(() => ({ ...baseConfig }), [baseConfig]);
163
+
164
+ <XmppProvider config={chatConfig}>
165
+ <Chat config={chatConfig} />
166
+ </XmppProvider>;
167
+ ```
168
+
141
169
  ### 3. Run
142
170
 
143
171
  ```bash
@@ -55,6 +55,24 @@ function wasHandledByFcm(id) {
55
55
  return id ? _handledByFcm.has(id) : false;
56
56
  }
57
57
 
58
+ function buildDedupeKey(payload = {}) {
59
+ const data = payload.data || {};
60
+ const notification = payload.notification || {};
61
+ const explicitMessageId =
62
+ payload.messageId ||
63
+ data.msgID ||
64
+ data.messageId ||
65
+ data.message_id ||
66
+ '';
67
+
68
+ const roomJid = data.jid || payload.roomJid || '';
69
+ const senderId = data.userJid || payload.senderId || '';
70
+ const title = notification.title || payload.title || '';
71
+ const body = notification.body || payload.body || payload.message || '';
72
+
73
+ return explicitMessageId || `${roomJid}|${senderId}|${title}|${body}`;
74
+ }
75
+
58
76
  // ── Helpers ───────────────────────────────────────────────────
59
77
  function isSystemPayload(data) {
60
78
  return !data?.msgID && !data?.jid && !!data?.userJid;
@@ -157,8 +175,14 @@ if (messaging) {
157
175
  const data = payload.data || {};
158
176
  const notification = payload.notification || {};
159
177
  const messageId = payload.messageId || data.msgID || null;
178
+ const dedupeKey = buildDedupeKey({
179
+ messageId,
180
+ data,
181
+ notification,
182
+ });
160
183
 
161
184
  markHandledByFcm(messageId);
185
+ markHandledByFcm(dedupeKey);
162
186
 
163
187
  const isSystem = isSystemPayload(data);
164
188
  const title =
@@ -244,12 +268,24 @@ self.addEventListener('push', (event) => {
244
268
  payloadData.messageId ||
245
269
  payloadData.message_id ||
246
270
  null;
271
+ const dedupeKey = buildDedupeKey({
272
+ messageId,
273
+ data: payloadData,
274
+ notification,
275
+ title: raw.title,
276
+ body: raw.body || raw.message,
277
+ roomJid: raw.roomJid,
278
+ senderId: raw.senderId,
279
+ });
247
280
 
248
- if (wasHandledByFcm(messageId)) {
281
+ if (wasHandledByFcm(messageId) || wasHandledByFcm(dedupeKey)) {
249
282
  // console.log(`[SW] push skipped – already handled by FCM (msgId=${messageId})`);
250
283
  return;
251
284
  }
252
285
 
286
+ markHandledByFcm(messageId);
287
+ markHandledByFcm(dedupeKey);
288
+
253
289
  if (sourceType !== 'json') {
254
290
  /* console.log('[SW][NON_JSON_PUSH_PROCESSED]', {
255
291
  sourceType,