Skip to content

Commit

Permalink
improve + documentation for relay responses in the websocket client
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastix committed Nov 5, 2024
1 parent 8c9d32c commit c8083fe
Showing 1 changed file with 33 additions and 3 deletions.
36 changes: 33 additions & 3 deletions src/Request/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use swentel\nostr\Event\Event;
use swentel\nostr\Message\AuthMessage;
use swentel\nostr\MessageInterface;
use swentel\nostr\Nip42\AuthEvent;
use swentel\nostr\Relay\Relay;
use swentel\nostr\Relay\RelaySet;
Expand Down Expand Up @@ -44,7 +45,7 @@ class Request implements RequestInterface
* Constructor for the Request class.
* Initializes the url and payload properties based on the provided websocket and message.
*/
public function __construct(Relay|RelaySet $relay, $message)
public function __construct(Relay|RelaySet $relay, MessageInterface $message)
{
if ($relay instanceof RelaySet) {
$this->relays = $relay;
Expand Down Expand Up @@ -105,6 +106,7 @@ private function getResponseFromRelay(Relay $relay): array | RelayResponse
$client->setTimeout(60);
$client->text($this->payload);

// The Nostr subscription lifecycle within a websocket connection lifecycle.
while ($response = $client->receive()) {
if ($response === null) {
$response = [
Expand All @@ -119,19 +121,41 @@ private function getResponseFromRelay(Relay $relay): array | RelayResponse
} elseif ($response instanceof Text) {
$relayResponse = RelayResponse::create(json_decode($response->getContent()));
$this->responses[] = $relayResponse;
// NIP-01 - Response OK from the relay.
if ($relayResponse->type === 'OK' && $relayResponse->status === false) {
// Something went wrong, see message from the relay why.
$client->disconnect();
throw new \Exception($relayResponse->message);
}
if ($relayResponse->type === 'OK' && $relayResponse->status === true) {
if (isset($relayResponse->eventId) && $relayResponse->eventId !== '') {
// Event is transmitted to the relay.
// TODO: send closeMessage to relay.
$client->disconnect();
break;
}
}
// NIP-01 - Response EVENT from the relay.
if ($relayResponse->type === 'EVENT') {
// Do nothing.
}
// NIP-01 - Response EOSE from the relay.
if ($relayResponse->type === 'EOSE') {
// We should send closeMessage to the relay here.
$client->disconnect();
break;
}
if ($relayResponse->type === 'OK' && $relayResponse->status === false) {
// We should send closeMessage to the relay here.
$client->disconnect();
throw new \Exception($relayResponse->message);
}
// NIP-42
// NIP-42 - Response AUTH from the relay.
if ($relayResponse->type === 'AUTH') {
// Save challenge string in session.
$_SESSION['challenge'] = $relayResponse->message;
}
// NIP-01 - Response CLOSED from the relay.
if ($relayResponse->type === 'CLOSED') {
// NIP-42
// We do need to broadcast a signed event verification here to the relay.
Expand Down Expand Up @@ -167,7 +191,9 @@ private function getResponseFromRelay(Relay $relay): array | RelayResponse
/** @var RelayResponse $response */
$response = RelayResponse::create(json_decode($message->getContent()));
$this->responses[] = $response;
$client->stop();
if ($response->type === 'EOSE') {
// We should send closeMessage to the relay here.
$client->disconnect();
}
})->start();
Expand All @@ -179,10 +205,14 @@ private function getResponseFromRelay(Relay $relay): array | RelayResponse
$client->disconnect();
throw new \Exception($relayResponse->message);
}
$client->disconnect();
break;
}
}
}
$client->disconnect();
if ($client->isConnected()) {
$client->disconnect();
}
$client->close();
return $this->responses;
}
Expand Down

0 comments on commit c8083fe

Please sign in to comment.