mirror of
https://github.com/HolgerHatGarKeineNode/einundzwanzig-nostr.git
synced 2025-12-22 15:40:15 +00:00
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:
@@ -41,14 +41,34 @@ trait NostrFetcherTrait
|
|||||||
|
|
||||||
$requestMessage = new RequestMessage($subscriptionId, $filters);
|
$requestMessage = new RequestMessage($subscriptionId, $filters);
|
||||||
|
|
||||||
$relayUrl = 'wss://relay.nostr.band/';
|
$relayUrls = [
|
||||||
$relay = new Relay($relayUrl);
|
'wss://relay.nostr.band',
|
||||||
$relay->setMessage($requestMessage);
|
'wss://purplepag.es',
|
||||||
|
'wss://nostr.wine',
|
||||||
|
'wss://relay.damus.io',
|
||||||
|
];
|
||||||
|
|
||||||
$request = new Request($relay, $requestMessage);
|
$data = null;
|
||||||
$response = $request->send();
|
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 {
|
try {
|
||||||
$result = json_decode($item->event->content, true, 512, JSON_THROW_ON_ERROR);
|
$result = json_decode($item->event->content, true, 512, JSON_THROW_ON_ERROR);
|
||||||
} catch (\JsonException $e) {
|
} catch (\JsonException $e) {
|
||||||
|
|||||||
Reference in New Issue
Block a user