nbd-wtf / nostr-tools

Tools for developing Nostr clients.
The Unlicense
685 stars 188 forks source link

Question: How can I subscribe to Encrypted Direct Message (NIP-04)? #394

Closed dicklarsson closed 5 months ago

dicklarsson commented 5 months ago

The README provides a brief and concise example of how to subscribe to an existing event. However, how can I subscribe to or listen for direct messages?

"As user ALICE, how can I subscribe to all incoming private messages?"

// let's query for an event that exists
const sub = relay.subscribe([
  {
    ids: ['d7dd5eb3ab747e16f8d0212d53032ea2a7cadef53837e5a6c66d42849fcb9027'],
  },
], {
  onevent(event) {
    console.log('we got the event we wanted:', event)
  },
  oneose() {
    sub.close()
  }
})
fiatjaf commented 5 months ago

I recommend that you read https://nips.nostr.com/1 and https://nips.nostr.com/4, but the answer is:

relay.subscribe([{"#p":["<receiver-pubkey>"],"kinds":[4]}, {"authors":["<sender-pubkey>"],"kinds":[4]}], ...)