python-nostr icon indicating copy to clipboard operation
python-nostr copied to clipboard

How to use add_subscription?

Open decentropy opened this issue 2 years ago • 6 comments

Can I call add_subscription after open_connections, or do I need to do it before like the README example?

Another question... Should I expect to get all old note/events using RelayManager.message_pool.has_event? I'm having trouble getting any thing, the while-loop just quits. I've even tried simplifying the filter to EventKind.TEXT_NOTE.

decentropy avatar Jul 14 '23 09:07 decentropy

did you make any progress on this. I'm having a hard time working with this -- which is because of my limited understanding & knowledge. But I just don't 'get' what is subscription ID - do I create it? If yes, how. All I've been able to do so far is connect to relays and post a note. I have read and re-read the readme but I still don't get it. Any help would be appreciated.

pleb21 avatar Jul 28 '23 06:07 pleb21

I wasn't able to figure it out, so I switched to https://github.com/holgern/pynostr

for subscription ID, it can be anything unique You could import uuid and then subid = uuid.uuid1().hex

decentropy avatar Jul 28 '23 18:07 decentropy

ok, thanks -- bookmarking it.

pleb21 avatar Jul 28 '23 18:07 pleb21

seems to work fine from the examples.

earonesty avatar Jul 30 '23 15:07 earonesty

tbh, I haven't tried it again after the suggestion from @decentropy - will comment here again if I run into issues. Thanks

pleb21 avatar Jul 30 '23 18:07 pleb21

Can I call add_subscription after open_connections, or do I need to do it before like the README example?

Just tested this, and seems to work either before or after:

relay_manager.open_connections()
relay_manager.add_subscription(subscription_id, filters)

for subscription ID, it can be anything unique

Yes, that works fine. Here's a complete example:

# Setup filters
filters = Filters([Filter(limit=10)])

# Generate subscription
subscription_id = uuid.uuid1().hex
request = [ClientMessageType.REQUEST, subscription_id]
request.extend(filters.to_json_array())

relay_manager = RelayManager()
relay_manager.add_relay("wss://nostr-pub.wellorder.net")
relay_manager.add_relay("wss://relay.damus.io")
relay_manager.add_relay("wss://nostr.wine")
relay_manager.add_relay("wss://nostr.rocks")
relay_manager.add_subscription(subscription_id, filters)
relay_manager.open_connections()

Should I expect to get all old note/events using RelayManager.message_pool.has_event?

I believe it's whatever the relay returns, based on your filters. So in theory yes - all old events.

franzos avatar Aug 01 '23 08:08 franzos