PHP code example of innis / nostr-relay-selection

1. Go to this page and download the library: Download innis/nostr-relay-selection library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

innis / nostr-relay-selection example snippets


use Innis\Nostr\RelaySelection\Domain\Entity\Event;
use Innis\Nostr\RelaySelection\Domain\Entity\Filter;
use Innis\Nostr\RelaySelection\Domain\ValueObject\Identity\PublicKey;
use Innis\Nostr\RelaySelection\Domain\ValueObject\Protocol\RelayUrl;
use Innis\Nostr\RelaySelection\Domain\ValueObject\Tag;

$event  = Event::tryFromRaw(json_decode($rawJson, true));   // ?Event
$filter = Filter::tryFromRaw(['kinds' => [1], 'search' => 'hi']);  // ?Filter
$tag    = Tag::tryFromRaw(['p', $pubkeyHex]);               // ?Tag
$pubkey = PublicKey::tryFromHex($hex);                      // ?PublicKey
$relay  = RelayUrl::tryFromString($url);                    // ?RelayUrl

use Innis\Nostr\RelaySelection\Domain\Service\PublishRouter;
use Innis\Nostr\RelaySelection\Domain\ValueObject\Context\PublishContext;
use Innis\Nostr\RelaySelection\Domain\ValueObject\Identity\PublicKey;
use Innis\Nostr\RelaySelection\Domain\Enum\Route\PublishBranch;

$context = new PublishContext(
    userPubkey: PublicKey::tryFromHex($userHex) ?? throw new InvalidArgumentException('bad pubkey'),
    relayListEvents: $cachedKind10002And10050Events,
    privateContentRelays: [],   // caller's pre-extracted kind 10013 URLs (see NIP-37 note below)
    indexerRelays: [],
    blockedRelays: $callerBlockedRelays,   // caller's pre-extracted kind 10006 URLs
);

$route = PublishRouter::route($event, $context);
$strategy = match ($route->getBranch()) {
    PublishBranch::General => 'NIP-65 outbox fan-out',
    PublishBranch::Dm      => 'NIP-17 DM inboxes (relays is null if the recipient has no kind 10050)',
    PublishBranch::Draft   => 'private content relays, falling back to the user outbox',
};
foreach ($route->getRelays() ?? [] as $relay) {
    $pool->publish((string) $relay, $event);
}

use Innis\Nostr\RelaySelection\Domain\Entity\Filter;
use Innis\Nostr\RelaySelection\Domain\Service\ReadRouter;
use Innis\Nostr\RelaySelection\Domain\ValueObject\Context\ReadContext;
use Innis\Nostr\RelaySelection\Domain\Enum\Route\ReadBranch;

$context = new ReadContext(
    userRelayUrls: $userOutboxRelays,
    callerRelays: $relaysPassedToTheQuery,
    filters: [new Filter(kinds: [1], pTags: null, search: null)],
    relayListEvents: $cachedRelayLists,
    blockedRelays: $callerBlockedRelays,    // pre-extracted kind 10006
    searchRelays: $callerSearchRelays,      // pre-extracted kind 10007
);

$route = ReadRouter::route($context);
$strategy = match ($route->getBranch()) {
    ReadBranch::Search   => 'subscribe to search-capable relays',
    ReadBranch::DmInbox  => "subscribe to the recipient's DM inboxes",
    ReadBranch::General  => 'subscribe to user + caller relays',
};

use Innis\Nostr\RelaySelection\Domain\Enum\Route\ReadBranch;
use Innis\Nostr\RelaySelection\Domain\Service\FilterPatternClassifier;

$branch = FilterPatternClassifier::classify($filters);
// ReadBranch::Search | ReadBranch::DmInbox | ReadBranch::General

use Innis\Nostr\RelaySelection\Domain\Service\AuthorReadRouter;
use Innis\Nostr\RelaySelection\Domain\ValueObject\Context\AuthorReadRouteContext;

$context = new AuthorReadRouteContext(
    authorPubkeys: $followedPubkeys,
    relayListEvents: $cachedRelayLists,
    fallbackRelays: $defaultRelays,
    maxAuthorsPerFilter: 200,
    redundancy: 3,
    blockedRelays: $callerBlockedRelays,
);

foreach (AuthorReadRouter::route($context) as $route) {
    foreach ($route->getAuthorChunks() as $chunk) {
        $pool->subscribe(
            array_map('strval', $route->getRelays()),
            ['kinds' => [1], 'authors' => array_map(fn ($pk) => $pk->toHex(), $chunk)],
        );
    }
}

$url = RelayUrl::tryFromString('ws://192.168.1.11:7777');
$url->isOnion();      // false
$url->isLoopback();   // false (true for localhost, 127.0.0.0/8, ::1)
$url->isLocalAddr();  // true  (loopback OR RFC1918 OR .local mDNS)
$url->isInsecure();   // true  (ws:// AND not onion)
bash
php example.php