rust-nostr/nostr

Subscription will send "REQ" twice

Ungigdu opened this issue · 5 comments

Hi @yukibtc
With the following test code, I find out the "REQ" been sent twice:

    #[wasm_bindgen_test(async)]
    async fn test_normal_handle() {
        let filter = Filter::new().limit(1).hashtag("lz");
        let db = WebDatabase::open("nostr-db").await.unwrap();
        let client_builder = ClientBuilder::new().database(db);
        let client = client_builder.build();
        // client.
        client.add_relay("wss://nos.lol").await.unwrap();
        // client.da
        client.connect().await;
        client.subscribe(vec![filter], None).await;

        client
        .handle_notifications(|notification| async move {
            console_log!("Received notification: {:?}", notification);
            Ok(false) // Set to true to exit from the loop
        }).await;
    }

There is what I saw in browser console:

image
    #[wasm_bindgen_test(async)]
    async fn test_normal_handle() {
        let filter = Filter::new().limit(1).hashtag("lz");
        let db = WebDatabase::open("nostr-db").await.unwrap();
        let client_builder = ClientBuilder::new().database(db);
        let client = client_builder.build();
        // client.
        client.add_relay("wss://nos.lol").await.unwrap();
        // client.da
        client.connect().await;
        client.subscribe(vec![filter], None).await;
        sleep(2000).await.unwrap();
    }

This will send "REQ" twice too.
Is it an issue in wasm?

Hi, @yukibtc ,
I have also tested this situation, and as @Ungigdu said, the subscription is indeed sent twice, which is a bit abnormal.

Thanks again! It's because after connection the relay automatically (re)subscribe to all added subscriptions. This to allow to keep up the subscriptions in the case of websocket disconnection and re-connection.

So, one of the REQ is sent immediately after connection (automatically) and the other one by the client.subscribe method call.

I'l try to search a solution.

I should have found a solution. Please, let me know if you notice any issues. To me seems working fine.

To test it update you Cargo.toml like this:

nostr-sdk = { git = "https://github.com/rust-nostr/nostr", rev = "96930e9f168ab5a95204e41fd8e2b12135943362" }

Yeah! This issue is fixed, thanks a million❤️