Aug 1, 2023

How to keep an indefinite NDK subscription on the server side?

#NDK noob question.

I'm trying to keep an indefinite subscription on the server side.

The issue here is that, slowly after receiving an eose, I'm being disconnected from the relay pool (pool stats show 0 connected relays).

Tried reconnecting on pool relay:disconnect (using ndk.connect() and ndk.pool.connect()) but it doesn't seem to be working.

Kinda running out of ideas on what's causing this. Perhaps I'm misunderstanding/missing something? Any tips on what I should look at/what to debug to find the issue?

Sharing the code below.

create ndk instance

const ndk = new NDK({ explicitRelayUrls: EXPLICIT_RELAYS, cacheAdapter: redisAdapter });

connect to relays

ndk.connect(2100)
    .then(() => {
        console.log(`Connected to relays`);
    });

create subscription

subscribe({
    kinds: [1, 30023],
    '#t': [Config.HASHTAG],
    since: Date.now() / 1000 - 45 * 60,
}, { closeOnEose: false, cacheUsage: NDKSubscriptionCacheUsage.PARALLEL, groupable: false});

subscribe function

const subscribe = (
    filter: NDKFilter,
    opts: NDKSubscriptionOptions
) => {

    const sub = ndk.subscribe(filter, opts);
    sub.on('event', onEvent);
    sub.on('eose', () => onEose);
    sub.on('close', () => onClose);
};

#asknostr #ndkdevkit #nostrdevs