Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add batchedList method to SimplePool #279

Merged
merged 1 commit into from
Aug 21, 2023

Conversation

sepehr-safari
Copy link
Contributor

Diff

  1. added a new optional parameter batchInterval to SimplePool class Default: 100ms
  2. added a new method batchedList to SimplePool class: it's similar to list() but with a batching mechanism.
  3. fix a bug to filter duplicate relays in sub()

batchedList() gets three parameters:

  1. batchKey as a string: used to group different subscriptions together into a single websocket request.
  2. relays: same as list method
  3. filters: same as list method

best practices:

  1. combine same relays and similar filters together with a unique batchKey e.g. authors notes contacts reactions it's totally up to you
  2. use batching for repetitive scenarios like getting the details of multiple profiles at the same time with the same and unique batchKey e.g. authors
  3. create a single SImplePool instance in your app wide and use it in different components at the same time. you can use a global state manager (on frontend frameworks eg React/Vue) based on your needs.

Example:

const App = () => {
  // subscribe to some relays and get some notes with kind 1
  const latestNotes = pool.list(relays, { kinds: [1], limit: 20 });
  // you don't have full details of the authors yet, just their pubkey
  // you will let each note component fetch its author details on its own

  return (
    <div>
      {latestNotes.map((note) => (
        <div key={note.id}>
          <NoteCard note={note} />
        </div>
      ))}
    </div>
  );
};

const NoteCard = ({ note }: { note: Note }) => {
  // fetch the author details with batchedList method with key 'authors'
  const author = pool.batchedList('authors', relays, {
    kinds: [0],
    limit: 1,
    authors: [note.author],
  });
  // SimplePool will batch all subscriptions with the same key together
  // and send JUST ONE request to each relay for each key
  // and then it will distribute the corresponding events to each subscription
  // so you can subscribe to the same key in different components
  // and you will get JUST DESIRED events for each component separately

  return (
    <div>
      <div>{note.content}</div>
      <div>{author.name}</div>
    </div>
  );
}

@fiatjaf fiatjaf merged commit 0925f5d into nbd-wtf:master Aug 21, 2023
1 check failed
@sepehr-safari
Copy link
Contributor Author

tnx @fiatjaf would you also consider publishing this version?

@fiatjaf
Copy link
Collaborator

fiatjaf commented Aug 21, 2023

Does it work?

@sepehr-safari
Copy link
Contributor Author

Does it work?

Y. like a butter 🤌🏻

@fiatjaf
Copy link
Collaborator

fiatjaf commented Aug 21, 2023

Published!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants