docs.waku.org/assets/js/5ae296d1.4e90ff7e.js
2025-10-03 05:55:52 +00:00

1 line
5.8 KiB
JavaScript

"use strict";(self.webpackChunkwaku_guide=self.webpackChunkwaku_guide||[]).push([[6408],{28453:(e,n,i)=>{i.d(n,{R:()=>o,x:()=>a});var t=i(96540);const s={},r=t.createContext(s);function o(e){const n=t.useContext(r);return t.useMemo(function(){return"function"==typeof e?e(n):{...n,...e}},[n,e])}function a(e){let n;return n=e.disableParentContext?"function"==typeof e.components?e.components(s):e.components||s:o(e.components),t.createElement(r.Provider,{value:n},e.children)}},86143:(e,n,i)=>{i.r(n),i.d(n,{assets:()=>l,contentTitle:()=>d,default:()=>h,frontMatter:()=>c,metadata:()=>t,toc:()=>u});const t=JSON.parse('{"id":"build/javascript/manage-filter","title":"Manage Your Filter Subscriptions","description":"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.","source":"@site/docs/build/javascript/manage-filter.md","sourceDirName":"build/javascript","slug":"/build/javascript/manage-filter","permalink":"/build/javascript/manage-filter","draft":false,"unlisted":false,"editUrl":"https://github.com/waku-org/docs.waku.org/tree/develop/docs/build/javascript/manage-filter.md","tags":[],"version":"current","lastUpdatedAt":null,"frontMatter":{"title":"Manage Your Filter Subscriptions","hide_table_of_contents":true,"displayed_sidebar":"build"},"sidebar":"build","previous":{"title":"Debug Your Waku DApp and WebSocket","permalink":"/build/javascript/debug-waku-dapp"},"next":{"title":"Frequently Asked Questions","permalink":"/build/javascript/faq"}}');var s=i(74848),r=i(28453);function o(e){const n={mermaid:"mermaid",...(0,r.R)(),...e.components};return(0,s.jsx)(n.mermaid,{value:"graph TD\n A[Start Monitoring Filter Subscriptions] --\x3e B{Check Peer Connection}\n B -- Connected --\x3e C[Send Ping]\n C --\x3e D{Ping Success?}\n D -- Yes --\x3e B\n D -- No --\x3e E[Handle Error/Reinitiate Subscription]\n B -- Disconnected --\x3e F[Check Intentional Disconnection/Unsubscription]\n F -- Yes --\x3e G[Stop Monitoring]\n F -- No --\x3e B\n E --\x3e B"})}function a(e={}){const{wrapper:n}={...(0,r.R)(),...e.components};return n?(0,s.jsx)(n,{...e,children:(0,s.jsx)(o,{...e})}):o(e)}const c={title:"Manage Your Filter Subscriptions",hide_table_of_contents:!0,displayed_sidebar:"build"},d=void 0,l={},u=[{value:"Overview",id:"overview",level:2},{value:"Pinging filter subscriptions",id:"pinging-filter-subscriptions",level:2}];function p(e){const n={a:"a",admonition:"admonition",code:"code",h2:"h2",p:"p",pre:"pre",...(0,r.R)(),...e.components};return(0,s.jsxs)(s.Fragment,{children:[(0,s.jsxs)(n.p,{children:["This guide provides detailed steps to manage ",(0,s.jsx)(n.a,{href:"/learn/concepts/protocols#filter",children:"Filter"})," subscriptions and handle node disconnections in your application. Have a look at the ",(0,s.jsx)(n.a,{href:"/build/javascript/light-send-receive",children:"Send and Receive Messages Using Light Push and Filter"})," guide for using the ",(0,s.jsx)(n.code,{children:"Light Push"})," and ",(0,s.jsx)(n.code,{children:"Filter"})," protocols."]}),"\n",(0,s.jsx)(n.h2,{id:"overview",children:"Overview"}),"\n",(0,s.jsxs)(n.p,{children:["Occasionally, your ",(0,s.jsx)(n.code,{children:"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 ",(0,s.jsx)(n.code,{children:'"peer has no subscriptions"'})," indicates a failed ping due to disconnection. You can stop the pings if the disconnection/unsubscription is deliberate."]}),"\n","\n",(0,s.jsx)(a,{}),"\n",(0,s.jsx)(n.h2,{id:"pinging-filter-subscriptions",children:"Pinging filter subscriptions"}),"\n",(0,s.jsxs)(n.p,{children:["The ",(0,s.jsx)(n.code,{children:"@waku/sdk"})," package provides a ",(0,s.jsx)(n.code,{children:"Filter.ping()"})," function to ping subscriptions and check for an active connection. To begin, create a ",(0,s.jsx)(n.code,{children:"Filter"})," subscription:"]}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-js",children:"// Create a Filter subscription\nconst { error, subscription } = await node.filter.createSubscription({ contentTopics: [contentTopic] });\n\nif (error) {\n // handle errors if happens\n throw Error(error);\n}\n\n// Subscribe to content topics and process new messages\nawait subscription.subscribe([decoder], callback);\n"})}),"\n",(0,s.jsx)(n.p,{children:"Next, create a function to ping and reinitiate the subscription:"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-js",children:'const pingAndReinitiateSubscription = async () => {\n\ttry {\n\t\t// Ping the subscription\n\t\tawait subscription.ping();\n\t} catch (error) {\n\t\tif (\n\t\t\t// Check if the error message includes "peer has no subscriptions"\n\t\t\terror instanceof Error &&\n\t\t\terror.message.includes("peer has no subscriptions")\n\t\t) {\n\t\t\t// Reinitiate the subscription if the ping fails\n\t\t\tawait subscription.subscribe([decoder], callback);\n\t\t} else {\n\t\t\tthrow error;\n\t\t}\n\t}\n};\n\n// Periodically ping the subscription\nawait pingAndReinitiateSubscription();\n'})}),"\n",(0,s.jsx)(n.admonition,{type:"info",children:(0,s.jsx)(n.p,{children:"Pings will fail when there are temporary network degradations or reachability issues. This does not mean that the underlying connection has been closed."})}),"\n",(0,s.jsx)(n.admonition,{title:"Congratulations!",type:"success",children:(0,s.jsxs)(n.p,{children:["You have successfully managed your ",(0,s.jsx)(n.code,{children:"Filter"})," subscriptions to handle node disconnections in your application."]})})]})}function h(e={}){const{wrapper:n}={...(0,r.R)(),...e.components};return n?(0,s.jsx)(n,{...e,children:(0,s.jsx)(p,{...e})}):p(e)}}}]);