nostrver-se / nostr-php

PHP helper library for Nostr https://nostr-php.dev
https://nostr-php.dev
MIT License
50 stars 15 forks source link

Add read from relay feature #55

Closed kriptonix closed 3 months ago

kriptonix commented 3 months ago

This code adds basic functionality to read events from Nostr relay. Send request to relay, get response and close WebSocket connection. For continuous WebSocket connection code needs to be additionally upgraded.

Create new subscription and set subscription ID. I use simple code to generate random 64 character ID for subscription:

$subscription = new Subscription();
$subscriptionId = $subscription->setId();

Define as many filters as you like:

$filter1 = new Filter();
$filter1->setAuthors(<array of pubkeys>);
$filter1->setKinds(<array of kinds>);
$filter1->setLimit(<integer>);
$filter1->setSince(<timestamp integer>);
$filter1->setUntil(<timestamp integer>);
$filter1->setETag(<array of #e tags>);
$filter1->setPTag(<array of #p tags>]);
$filter2 = new Filter();
$filter2->setKinds([1]);
$filter2->setLimit(3);

Combine all filters into one array: $filters = [$filter1, $filter2];

Create request message: $requestMessage = new RequestMessage($subscriptionId, $filters);

Create request and send it to relay:

$request = new Request($relayUrl, $requestMessage);
$result = $request->send();