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

receivedEvent called but not onevent #466

Open
dustinb opened this issue Jan 4, 2025 · 2 comments
Open

receivedEvent called but not onevent #466

dustinb opened this issue Jan 4, 2025 · 2 comments

Comments

@dustinb
Copy link

dustinb commented Jan 4, 2025

Are there event filters before onevent is called? I'm seeing the events on the websocket and receivedEvent is called but onevent is not for the messages.

  useEffect(() => {
    const connectRelay = async () => {
      const relay = await Relay.connect("ws://localhost:8080");
      setRelay(relay);
    };

    connectRelay();

    return () => {
      if (relay) {
        relay.close();
      }
    };
  }, []);

  // subscribe to some events
  useEffect(() => {
    if (!relay) return;

    setEvents([]);

    const filter: Filter = {
      kinds: [30402],
      limit: 5,
    };
    if (hashtags.length > 0) {
      filter["#t"] = hashtags;
    }
    
    const sub = relay.subscribe([filter], {
      alreadyHaveEvent: (id) => {
        return false;
      },
      receivedEvent: (relay, id) => {
        console.log("Received event:", relay, id);
      },
      onevent(event) {
        console.log("onevent:", event.id);
        setEvents(prev => [...prev, event]);
      },
      oneose() {
        console.log("EOSE");
      }
    });

    return () => {
      console.log("Cleaning up subscription");
      sub.close();
    };
  }, [hashtags, relay]);

and console log shows only receivedEvent

image

@fiatjaf
Copy link
Collaborator

fiatjaf commented Jan 4, 2025

onevent is only called once for each event, we only parse the event once and ignore duplicates. receivedEvent is called for everything, but only with the id and relay it came from.

From your logs it looks like you're closing the subscription you have before the events arrive, so onevent can't be called anymore.

@dustinb
Copy link
Author

dustinb commented Jan 5, 2025

That cleaning up log is from the previous subscription. A new one is created anytime the hashtags change. Is the duplicate check at the relay level or subscription? If previous events persisted at the relay level that might explain why onevent is not called.

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

No branches or pull requests

2 participants