Manage Your Filter Subscriptions
This guide provides detailed steps to manage Filter subscriptions and handle node disconnections in your application. Have a look at the Send and Receive Messages Using Light Push and Filter guide for using the Light Push
and Filter
protocols.
Overview
Occasionally, your Filter
subscriptions might disconnect from the Waku Network, resulting in messages not being received by your application. To manage your subscriptions, periodically ping peers to check for an active connection. The error message "peer has no subscriptions"
indicates a failed ping due to disconnection. You can stop the pings if the disconnection/unsubscription is deliberate.
Pinging filter subscriptions
The @waku/sdk
package provides a Filter.ping()
function to ping subscriptions and check for an active connection. To begin, create a Filter
subscription:
// Create a Filter subscription
const { error, subscription } = await node.filter.createSubscription({ contentTopics: [contentTopic] });
if (error) {
// handle errors if happens
throw Error(error);
}
// Subscribe to content topics and process new messages
await subscription.subscribe([decoder], callback);
Next, create a function to ping and reinitiate the subscription:
const pingAndReinitiateSubscription = async () => {
try {
// Ping the subscription
await subscription.ping();
} catch (error) {
if (
// Check if the error message includes "peer has no subscriptions"
error instanceof Error &&
error.message.includes("peer has no subscriptions")
) {
// Reinitiate the subscription if the ping fails
await subscription.subscribe([decoder], callback);
} else {
throw error;
}
}
};
// Periodically ping the subscription
await pingAndReinitiateSubscription();
Pings will fail when there are temporary network degradations or reachability issues. This does not mean that the underlying connection has been closed.
You have successfully managed your Filter
subscriptions to handle node disconnections in your application.