Skip to content

bug: gift wrap subscription reconnect causes missed messages (no backfill on reconnect) #592

@islandbitcoin

Description

@islandbitcoin

Summary

When the gift wrap subscription (fetchGiftWrapsForPublicKey) drops and reconnects, any NIP-17 gift wraps published during the reconnect window are permanently missed. There is no backfill mechanism to catch up on events received while the subscription was down.

Root Cause

In app/utils/nostr.ts, the subscription reconnect logic re-establishes the connection but starts listening from now with no since filter:

onclose: () => {
  closer.close()
  closer = fetchGiftWrapsForPublicKey(pubkey, eventHandler, pool)
},

The new subscription call has no since timestamp, so any events published during the gap between close and reconnect are never fetched.

Impact

  • Messages sent while the recipient's subscription is reconnecting are silently dropped
  • Manifests as "Missed a note" / "Missed N notes" in the chat UI
  • Intermittent — depends on relay connection stability and timing
  • Observed during QA testing of PR QOL features on chat #579

Suggested Fix

Track the last event timestamp received (lastSeenAt) and pass it as a since filter on reconnect:

let lastSeenAt = Math.floor(Date.now() / 1000)

onclose: () => {
  closer.close()
  closer = fetchGiftWrapsForPublicKey(pubkey, eventHandler, pool, lastSeenAt)
},

// In the subscription:
onevent: (event) => {
  lastSeenAt = Math.max(lastSeenAt, event.created_at)
  eventHandler(event)
}

This ensures reconnects backfill any events missed during the gap.

Steps to Reproduce

  1. Open two accounts in chat (iOS simulator + Android emulator)
  2. Send messages back and forth rapidly
  3. Observe "Missed a note" appearing on one side
  4. Check Metro logs — subscription reconnect events coincide with missed messages

References

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions