|
| 1 | +--- |
| 2 | +title: Manage Your Filter Subscriptions |
| 3 | +--- |
| 4 | + |
| 5 | +This guide provides detailed steps to manage [Filter](/overview/concepts/protocols#filter) subscriptions and handle node disconnections in your application. Have a look at the [Filter guide](/guides/js-waku/light-send-receive) for receiving messages with the `Light Push and Filter protocol`. |
| 6 | + |
| 7 | +## Overview |
| 8 | + |
| 9 | +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. |
| 10 | + |
| 11 | +```mdx-code-block |
| 12 | +import FilterPingFlow from "@site/diagrams/_filter-ping-flow.md"; |
| 13 | +
|
| 14 | +<FilterPingFlow /> |
| 15 | +``` |
| 16 | + |
| 17 | +## Pinging Filter Subscriptions |
| 18 | + |
| 19 | +The `@waku/sdk` package provides a `Filter.ping()` function to ping subscriptions and check for an active connection. To begin, create a `Filter` subscription: |
| 20 | + |
| 21 | +```js |
| 22 | +// Create a Filter subscription |
| 23 | +const subscription = await node.filter.createSubscription(); |
| 24 | + |
| 25 | +// Subscribe to content topics and process new messages |
| 26 | +await subscription.subscribe([decoder], callback); |
| 27 | +``` |
| 28 | + |
| 29 | +Next, create a function to ping and reinitiate the subscription: |
| 30 | + |
| 31 | +```js |
| 32 | +const pingAndReinitiateSubscription = async () => { |
| 33 | + try { |
| 34 | + // Ping the subscription |
| 35 | + await subscription.ping(); |
| 36 | + } catch (error) { |
| 37 | + if ( |
| 38 | + // Check if the error message includes "peer has no subscriptions" |
| 39 | + error instanceof Error && |
| 40 | + error.message.includes("peer has no subscriptions") |
| 41 | + ) { |
| 42 | + // Reinitiate the subscription if the ping fails |
| 43 | + await subscription.subscribe([decoder], callback); |
| 44 | + } else { |
| 45 | + throw error; |
| 46 | + } |
| 47 | + } |
| 48 | +}; |
| 49 | + |
| 50 | +// Periodically ping the subscription |
| 51 | +await pingAndReinitiateSubscription(); |
| 52 | +``` |
| 53 | + |
| 54 | +:::success Congratulations! |
| 55 | +You have successfully managed your `Filter` subscriptions to handle node disconnections in your application. |
| 56 | +::: |
0 commit comments