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.
This commit is contained in:
fsociety
2024-09-30 20:52:20 +02:00
parent 883bd4d1e5
commit 6ac364286d

View File

@@ -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) {