From 6ac364286df7c5e6d9abdc3cb9d4386e048b2285 Mon Sep 17 00:00:00 2001 From: fsociety Date: Mon, 30 Sep 2024 20:52:20 +0200 Subject: [PATCH] feat: add multiple relay URLs in NostrFetcherTrait This commit introduces support for multiple relay URLs in the NostrFetcherTrait. The application will now attempt to fetch data from a list of relay URLs instead of just one. This change provides a better failover mechanism in case one of the relay servers is down or unresponsive. The application will now throw a RuntimeException if no data is received from any of the relays. --- app/Traits/NostrFetcherTrait.php | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/app/Traits/NostrFetcherTrait.php b/app/Traits/NostrFetcherTrait.php index 963d944..2fcbe74 100644 --- a/app/Traits/NostrFetcherTrait.php +++ b/app/Traits/NostrFetcherTrait.php @@ -41,14 +41,34 @@ trait NostrFetcherTrait $requestMessage = new RequestMessage($subscriptionId, $filters); - $relayUrl = 'wss://relay.nostr.band/'; - $relay = new Relay($relayUrl); - $relay->setMessage($requestMessage); + $relayUrls = [ + 'wss://relay.nostr.band', + 'wss://purplepag.es', + 'wss://nostr.wine', + 'wss://relay.damus.io', + ]; - $request = new Request($relay, $requestMessage); - $response = $request->send(); + $data = null; + foreach ($relayUrls as $relayUrl) { + $relay = new Relay($relayUrl); + $relay->setMessage($requestMessage); + $request = new Request($relay, $requestMessage); + try { + $response = $request->send(); + $data = $response[$relayUrl]; + if (!empty($data)) { + break; // Exit the loop if data is not empty + } + } catch (\Exception $e) { + // Log the exception or handle it if needed + } + } - foreach ($response['wss://relay.nostr.band/'] as $item) { + if (empty($data)) { + throw new \RuntimeException('No data received from any relay.'); + } + + foreach ($data as $item) { try { $result = json_decode($item->event->content, true, 512, JSON_THROW_ON_ERROR); } catch (\JsonException $e) {