Skip to main content

Picking relays of the people the logged-in user follows

Picking the "best" relay for each followed user

  1. Pick a random r tag value (which is a relay URL) from the kind 10002 event of the logged-in user

  2. Collect all p tag values from the kind 3 event of the logged-in user into an array

    {
    "kind": 3,
    "tags": [
    ["p", "pubkey1"],
    ["p", "pubkey2"],
    ["p", "pubkey3"]
    ]
    }
  3. Then use the relay from 1. to simulate a traditional request-response flow via an ad-hoc subscription with 1 filters JSON:

    {"kinds":[10002],"authors":["pubkey1","pubkey2","pubkey3"]}
    note

    The kind 10002 events returned should be cached for a reasonable amount of time to avoid re-requesting them all the time

    note

    Another useful information to be cached is the aggregation/grouping of the r tags of those events and their pubkeys as such so that the "best" relay for each pubkey is picked; "best" being the relay present in most of the returned events.

    I.E. For the following kind 10002 events returned:

    {
    "pubkey": "pubkey1",
    "kind": 10002,
    "tags": [
    ["r", "wss://relay1.example.com"],
    ["r", "wss://relay2.example.com"],
    ["r", "wss://relay3.example.com"],
    ["r", "wss://relay4.example.com"]
    ]
    }
    {
    "pubkey": "pubkey2",
    "kind": 10002,
    "tags": [
    ["r", "wss://relay4.example.com"],
    ["r", "wss://relay5.example.com"],
    ["r", "wss://relay6.example.com"],
    ["r", "wss://relay7.example.com"]
    ]
    }
    {
    "pubkey": "pubkey3",
    "kind": 10002,
    "tags": [
    ["r", "wss://relay8.example.com"],
    ["r", "wss://relay9.example.com"],
    ["r", "wss://relay10.example.com"],
    ["r", "wss://relay11.example.com"]
    ]
    }

    The "best" relay aggregation result should be:

    relay4.example.com: pubkey1,pubkey2
    relay10.example.com: pubkey3 -- picked randomly because none of them are used by anyone else