mirror of
https://github.com/logos-messaging/docs.waku.org.git
synced 2026-01-03 21:33:05 +00:00
1 line
14 KiB
JavaScript
1 line
14 KiB
JavaScript
"use strict";(self.webpackChunkwaku_guide=self.webpackChunkwaku_guide||[]).push([[2285],{3905:(e,t,r)=>{r.d(t,{Zo:()=>p,kt:()=>g});var n=r(67294);function a(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function o(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),r.push.apply(r,n)}return r}function s(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?o(Object(r),!0).forEach((function(t){a(e,t,r[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):o(Object(r)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))}))}return e}function i(e,t){if(null==e)return{};var r,n,a=function(e,t){if(null==e)return{};var r,n,a={},o=Object.keys(e);for(n=0;n<o.length;n++)r=o[n],t.indexOf(r)>=0||(a[r]=e[r]);return a}(e,t);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(e);for(n=0;n<o.length;n++)r=o[n],t.indexOf(r)>=0||Object.prototype.propertyIsEnumerable.call(e,r)&&(a[r]=e[r])}return a}var l=n.createContext({}),c=function(e){var t=n.useContext(l),r=t;return e&&(r="function"==typeof e?e(t):s(s({},t),e)),r},p=function(e){var t=c(e.components);return n.createElement(l.Provider,{value:t},e.children)},d="mdxType",u={inlineCode:"code",wrapper:function(e){var t=e.children;return n.createElement(n.Fragment,{},t)}},m=n.forwardRef((function(e,t){var r=e.components,a=e.mdxType,o=e.originalType,l=e.parentName,p=i(e,["components","mdxType","originalType","parentName"]),d=c(r),m=a,g=d["".concat(l,".").concat(m)]||d[m]||u[m]||o;return r?n.createElement(g,s(s({ref:t},p),{},{components:r})):n.createElement(g,s({ref:t},p))}));function g(e,t){var r=arguments,a=t&&t.mdxType;if("string"==typeof e||a){var o=r.length,s=new Array(o);s[0]=m;var i={};for(var l in t)hasOwnProperty.call(t,l)&&(i[l]=t[l]);i.originalType=e,i[d]="string"==typeof e?e:a,s[1]=i;for(var c=2;c<o;c++)s[c]=r[c];return n.createElement.apply(null,s)}return n.createElement.apply(null,r)}m.displayName="MDXCreateElement"},55345:(e,t,r)=>{r.r(t),r.d(t,{assets:()=>l,contentTitle:()=>s,default:()=>u,frontMatter:()=>o,metadata:()=>i,toc:()=>c});var n=r(87462),a=(r(67294),r(3905));const o={title:"Retrieve Messages Using Store Protocol",hide_table_of_contents:!0},s=void 0,i={unversionedId:"guides/js-waku/store-retrieve-messages",id:"guides/js-waku/store-retrieve-messages",title:"Retrieve Messages Using Store Protocol",description:"This guide provides detailed steps to create a Light Node for retrieving and filtering historical messages using the Store protocol.",source:"@site/docs/guides/js-waku/store-retrieve-messages.md",sourceDirName:"guides/js-waku",slug:"/guides/js-waku/store-retrieve-messages",permalink:"/guides/js-waku/store-retrieve-messages",draft:!1,editUrl:"https://github.com/waku-org/docs.waku.org/tree/develop/docs/guides/js-waku/store-retrieve-messages.md",tags:[],version:"current",lastUpdatedAt:1728641107,formattedLastUpdatedAt:"11 Oct 2024",frontMatter:{title:"Retrieve Messages Using Store Protocol",hide_table_of_contents:!0},sidebar:"guides",previous:{title:"Send and Receive Messages Using Light Push and Filter",permalink:"/guides/js-waku/light-send-receive"},next:{title:"Encrypt, Decrypt, and Sign Your Messages",permalink:"/guides/js-waku/message-encryption"}},l={},c=[{value:"Create a light node",id:"create-a-light-node",level:2},{value:"Connect to store peers",id:"connect-to-store-peers",level:2},{value:"Choose a content topic",id:"choose-a-content-topic",level:2},{value:"Retrieve messages",id:"retrieve-messages",level:2},{value:"<code>queryWithOrderedCallback</code>",id:"querywithorderedcallback",level:3},{value:"<code>queryGenerator</code>",id:"querygenerator",level:3},{value:"Store query options",id:"store-query-options",level:2},{value:"<code>pageDirection</code>",id:"pagedirection",level:3},{value:"<code>cursor</code>",id:"cursor",level:3},{value:"<code>timeFilter</code>",id:"timefilter",level:3}],p={toc:c},d="wrapper";function u(e){let{components:t,...r}=e;return(0,a.kt)(d,(0,n.Z)({},p,r,{components:t,mdxType:"MDXLayout"}),(0,a.kt)("p",null,"This guide provides detailed steps to create a Light Node for retrieving and filtering historical messages using the ",(0,a.kt)("a",{parentName:"p",href:"/learn/concepts/protocols#store"},"Store protocol"),"."),(0,a.kt)("h2",{id:"create-a-light-node"},"Create a light node"),(0,a.kt)("p",null,"Use the ",(0,a.kt)("inlineCode",{parentName:"p"},"createLightNode()")," function to create a Light Node and interact with the Waku Network:"),(0,a.kt)("pre",null,(0,a.kt)("code",{parentName:"pre",className:"language-js"},'import { createLightNode } from "@waku/sdk";\n\n// Create and start a Light Node\nconst node = await createLightNode({ defaultBootstrap: true });\nawait node.start();\n')),(0,a.kt)("h2",{id:"connect-to-store-peers"},"Connect to store peers"),(0,a.kt)("p",null,"Use the ",(0,a.kt)("inlineCode",{parentName:"p"},"node.waitForPeers()")," method to wait for the node to connect with Store peers:"),(0,a.kt)("pre",null,(0,a.kt)("code",{parentName:"pre",className:"language-js"},'import { Protocols } from "@waku/sdk";\n\n// Wait for a successful peer connection\nawait node.waitForPeers([Protocols.Store]);\n')),(0,a.kt)("h2",{id:"choose-a-content-topic"},"Choose a content topic"),(0,a.kt)("p",null,(0,a.kt)("a",{parentName:"p",href:"/learn/concepts/content-topics"},"Choose a content topic")," for filtering the messages to retrieve and create a message ",(0,a.kt)("inlineCode",{parentName:"p"},"decoder"),":"),(0,a.kt)("pre",null,(0,a.kt)("code",{parentName:"pre",className:"language-js"},'import { createDecoder } from "@waku/sdk";\n\n// Choose a content topic\nconst contentTopic = "/store-guide/1/message/proto";\n\n// Create a message decoder\nconst decoder = createDecoder(contentTopic);\n')),(0,a.kt)("h2",{id:"retrieve-messages"},"Retrieve messages"),(0,a.kt)("p",null,(0,a.kt)("inlineCode",{parentName:"p"},"@waku/sdk")," provides the ",(0,a.kt)("inlineCode",{parentName:"p"},"queryWithOrderedCallback()")," and ",(0,a.kt)("inlineCode",{parentName:"p"},"queryGenerator()")," functions for querying ",(0,a.kt)("inlineCode",{parentName:"p"},"Store")," nodes and retrieving historical or missed messages. The responses from ",(0,a.kt)("inlineCode",{parentName:"p"},"Store")," nodes are paginated and require you to process each page sequentially."),(0,a.kt)("h3",{id:"querywithorderedcallback"},(0,a.kt)("inlineCode",{parentName:"h3"},"queryWithOrderedCallback")),(0,a.kt)("p",null,"The ",(0,a.kt)("inlineCode",{parentName:"p"},"store.queryWithOrderedCallback()")," function provides a straightforward method for querying ",(0,a.kt)("inlineCode",{parentName:"p"},"Store")," nodes and processing messages in chronological order through a callback function. It accepts these parameters:"),(0,a.kt)("ul",null,(0,a.kt)("li",{parentName:"ul"},(0,a.kt)("inlineCode",{parentName:"li"},"decoders"),": List of ",(0,a.kt)("inlineCode",{parentName:"li"},"decoders")," that specify the ",(0,a.kt)("inlineCode",{parentName:"li"},"content topic")," to query for and their ",(0,a.kt)("a",{parentName:"li",href:"https://rfc.vac.dev/waku/standards/application/26/payload"},"message decryption")," methods."),(0,a.kt)("li",{parentName:"ul"},(0,a.kt)("inlineCode",{parentName:"li"},"callback"),": The callback function for processing the retrieved messages."),(0,a.kt)("li",{parentName:"ul"},(0,a.kt)("inlineCode",{parentName:"li"},"options")," (optional): ",(0,a.kt)("a",{parentName:"li",href:"/guides/js-waku/store-retrieve-messages#store-query-options"},"Query options")," to filter the retrieved messages.")),(0,a.kt)("pre",null,(0,a.kt)("code",{parentName:"pre",className:"language-js"},"// Create the callback function\nconst callback = (wakuMessage) => {\n // Render the message/payload in your application\n console.log(wakuMessage);\n};\n\n// Query the Store peer\nawait node.store.queryWithOrderedCallback([decoder], callback);\n")),(0,a.kt)("admonition",{type:"info"},(0,a.kt)("p",{parentName:"admonition"},"The ",(0,a.kt)("inlineCode",{parentName:"p"},"queryWithOrderedCallback()")," function always returns the most recent messages in a page first.")),(0,a.kt)("h3",{id:"querygenerator"},(0,a.kt)("inlineCode",{parentName:"h3"},"queryGenerator")),(0,a.kt)("p",null,"The ",(0,a.kt)("inlineCode",{parentName:"p"},"store.queryGenerator()")," function provides more control and flexibility over processing messages retrieved from ",(0,a.kt)("inlineCode",{parentName:"p"},"Store")," nodes through ",(0,a.kt)("a",{parentName:"p",href:"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AsyncGenerator"},"Async Generators"),". It accepts these parameters:"),(0,a.kt)("ul",null,(0,a.kt)("li",{parentName:"ul"},(0,a.kt)("inlineCode",{parentName:"li"},"decoders"),": List of ",(0,a.kt)("inlineCode",{parentName:"li"},"decoders")," that specify the ",(0,a.kt)("inlineCode",{parentName:"li"},"content topic")," to query for and their ",(0,a.kt)("a",{parentName:"li",href:"https://rfc.vac.dev/waku/standards/application/26/payload"},"message decryption")," methods."),(0,a.kt)("li",{parentName:"ul"},(0,a.kt)("inlineCode",{parentName:"li"},"options")," (optional): ",(0,a.kt)("a",{parentName:"li",href:"/guides/js-waku/store-retrieve-messages#store-query-options"},"Query options")," to filter the retrieved messages.")),(0,a.kt)("pre",null,(0,a.kt)("code",{parentName:"pre",className:"language-js"},"// Create the store query\nconst storeQuery = node.store.queryGenerator([decoder]);\n\n// Process the messages\nfor await (const messagesPromises of storeQuery) {\n // Fulfil the messages promises\n const messages = await Promise.all(\n messagesPromises.map(async (p) => {\n const msg = await p;\n // Render the message/payload in your application\n console.log(msg);\n })\n );\n}\n")),(0,a.kt)("admonition",{type:"info"},(0,a.kt)("p",{parentName:"admonition"},"The ",(0,a.kt)("inlineCode",{parentName:"p"},"queryGenerator()")," function always returns the oldest messages in a page first.")),(0,a.kt)("h2",{id:"store-query-options"},"Store query options"),(0,a.kt)("h3",{id:"pagedirection"},(0,a.kt)("inlineCode",{parentName:"h3"},"pageDirection")),(0,a.kt)("p",null,"The ",(0,a.kt)("inlineCode",{parentName:"p"},"pageDirection")," option specifies the direction in which pages are retrieved:"),(0,a.kt)("ul",null,(0,a.kt)("li",{parentName:"ul"},(0,a.kt)("inlineCode",{parentName:"li"},"BACKWARD")," (default): Most recent page first."),(0,a.kt)("li",{parentName:"ul"},(0,a.kt)("inlineCode",{parentName:"li"},"FORWARD"),": Oldest page first.")),(0,a.kt)("pre",null,(0,a.kt)("code",{parentName:"pre",className:"language-js"},'import { PageDirection } from "@waku/sdk";\n\n// Retrieve recent messages first\nconst queryOptions = {\n pageDirection: PageDirection.BACKWARD,\n};\n\n// Retrieve oldest messages first\nconst queryOptions = {\n pageDirection: PageDirection.FORWARD,\n};\n\n// Query the Store peer with options\nawait node.store.queryWithOrderedCallback([decoder], callback, options);\nconst storeQuery = node.store.queryGenerator([decoder, options]);\n')),(0,a.kt)("h3",{id:"cursor"},(0,a.kt)("inlineCode",{parentName:"h3"},"cursor")),(0,a.kt)("p",null,"The ",(0,a.kt)("inlineCode",{parentName:"p"},"cursor")," option specifies the starting index for retrieving messages. For example, consider a query that retrieves the first page messages and then continues with the next page:"),(0,a.kt)("pre",null,(0,a.kt)("code",{parentName:"pre",className:"language-js"},'import { waku } from "@waku/sdk";\n\n// Create the callback function\nconst messages = [];\nconst callback = (wakuMessage) => {\n messages.push(wakuMessage);\n // Return "true" to stop retrieving pages\n // Here, it retrieves only the first page\n return true;\n};\n\n// Retrieve the first page of messages\n// This retrieves all the messages if "return true" is not present\nawait node.store.queryWithOrderedCallback([decoder], callback);\n\n// Create the cursor\nconst lastMessage = messages[messages.length - 1];\nconst cursor = await waku.createCursor(lastMessage);\n\n// Retrieve the next page of messages\n// The message at the cursor index is excluded from the result\nawait node.store.queryWithOrderedCallback([decoder], callback, {\n cursor: cursor,\n});\nconsole.log(messages);\n')),(0,a.kt)("admonition",{type:"info"},(0,a.kt)("p",{parentName:"admonition"},"If you omit the ",(0,a.kt)("inlineCode",{parentName:"p"},"cursor")," option, the query will start from the beginning or end of the history, depending on the ",(0,a.kt)("a",{parentName:"p",href:"#pagedirection"},"page direction"),".")),(0,a.kt)("h3",{id:"timefilter"},(0,a.kt)("inlineCode",{parentName:"h3"},"timeFilter")),(0,a.kt)("p",null,"The ",(0,a.kt)("inlineCode",{parentName:"p"},"timeFilter")," option specifies a time frame to retrieve messages from. For example, consider a query that retrieves messages from the previous week:"),(0,a.kt)("pre",null,(0,a.kt)("code",{parentName:"pre",className:"language-js"},"// Get the time frame\nconst endTime = new Date();\nconst startTime = new Date();\nstartTime.setDate(endTime.getDate() - 7);\n\n// Retrieve a week of messages\nconst queryOptions = {\n timeFilter: {\n startTime,\n endTime,\n },\n};\n\n// Query the Store peer with options\nawait node.store.queryWithOrderedCallback([decoder], callback, options);\nconst storeQuery = node.store.queryGenerator([decoder, options]);\n")),(0,a.kt)("admonition",{type:"info"},(0,a.kt)("p",{parentName:"admonition"},"The ",(0,a.kt)("inlineCode",{parentName:"p"},"timeFilter")," option significantly reduces message retrieval performance. To optimise it, consider resuming message retrieval using a ",(0,a.kt)("a",{parentName:"p",href:"#cursor"},"cursor")," that starts from the last seen message.")),(0,a.kt)("admonition",{title:"Congratulations!",type:"tip"},(0,a.kt)("p",{parentName:"admonition"},"You have successfully retrieved and filtered historical messages on a Light Node using the ",(0,a.kt)("inlineCode",{parentName:"p"},"Store")," protocol. Have a look at the ",(0,a.kt)("a",{parentName:"p",href:"https://github.com/waku-org/js-waku-examples/tree/master/examples/store-js"},"store-js")," and ",(0,a.kt)("a",{parentName:"p",href:"https://github.com/waku-org/js-waku-examples/tree/master/examples/store-reactjs-chat"},"store-reactjs-chat")," examples for working demos.")))}u.isMDXComponent=!0}}]); |