resubscribe when a new relay is added

This commit is contained in:
callebtc 2023-04-11 15:47:21 +02:00
parent 0ffb158769
commit e0938cb760
3 changed files with 20 additions and 4 deletions

View file

@ -2,6 +2,4 @@
`nostrclient` is an always-on extension that can open multiple connections to nostr relays and act as a multiplexer for other clients: You open a single websocket to `nostrclient` which then sends the data to multiple relays. The responses from these relays are then sent back to the client. `nostrclient` is an always-on extension that can open multiple connections to nostr relays and act as a multiplexer for other clients: You open a single websocket to `nostrclient` which then sends the data to multiple relays. The responses from these relays are then sent back to the client.
![2023-03-08 18 11 07](https://user-images.githubusercontent.com/93376500/225265727-369f0f8a-196e-41df-a0d1-98b50a0228be.jpg) ![2023-03-08 18 11 07](https://user-images.githubusercontent.com/93376500/225265727-369f0f8a-196e-41df-a0d1-98b50a0228be.jpg)

View file

@ -45,7 +45,6 @@ class NostrRouter:
except WebSocketDisconnect: except WebSocketDisconnect:
self.connected = False self.connected = False
break break
# print(json_str)
# registers a subscription if the input was a REQ request # registers a subscription if the input was a REQ request
subscription_id, json_str_rewritten = await self._add_nostr_subscription( subscription_id, json_str_rewritten = await self._add_nostr_subscription(
@ -81,7 +80,7 @@ class NostrRouter:
} }
# this reconstructs the original response from the relay # this reconstructs the original response from the relay
# reconstruct oriiginal subscription id # reconstruct original subscription id
s_original = s[len(f"{self.subscription_id_rewrite}_") :] s_original = s[len(f"{self.subscription_id_rewrite}_") :]
event_to_forward = ["EVENT", s_original, event_json] event_to_forward = ["EVENT", s_original, event_json]
# print(json.dumps(event_to_forward)) # print(json.dumps(event_to_forward))
@ -104,6 +103,7 @@ class NostrRouter:
async def stop(self): async def stop(self):
for t in self.tasks: for t in self.tasks:
t.cancel() t.cancel()
self.connected = False
def _marshall_nostr_filters(self, data: Union[dict, list]): def _marshall_nostr_filters(self, data: Union[dict, list]):
filters = data if isinstance(data, list) else [data] filters = data if isinstance(data, list) else [data]

View file

@ -1,5 +1,6 @@
import asyncio import asyncio
import ssl import ssl
import json
import threading import threading
from .nostr.event import Event from .nostr.event import Event
@ -17,6 +18,13 @@ from .crud import get_relays
async def init_relays(): async def init_relays():
# we save any subscriptions teporarily to re-add them after reinitializing the client
subscriptions = {}
for relay in nostr.client.relay_manager.relays.values():
# relay.add_subscription(id, filters)
for subscription_id, filters in relay.subscriptions.items():
subscriptions[subscription_id] = filters
# reinitialize the entire client # reinitialize the entire client
nostr.__init__() nostr.__init__()
# get relays from db # get relays from db
@ -24,6 +32,16 @@ async def init_relays():
# set relays and connect to them # set relays and connect to them
nostr.client.relays = list(set([r.url for r in relays.__root__ if r.url])) nostr.client.relays = list(set([r.url for r in relays.__root__ if r.url]))
nostr.client.connect() nostr.client.connect()
await asyncio.sleep(2)
# re-add subscriptions
for subscription_id, subscription in subscriptions.items():
nostr.client.relay_manager.add_subscription(
subscription_id, subscription.filters
)
s = subscription.to_json_object()
json_str = json.dumps(["REQ", s["id"], s["filters"][0]])
nostr.client.relay_manager.publish_message(json_str)
return return