mirror of
https://github.com/waku-org/js-waku.git
synced 2025-02-18 15:26:50 +00:00
Add and update time filter guides
This commit is contained in:
parent
d6f180cf36
commit
0d82b74854
@ -266,16 +266,16 @@ Note that `WakuStore.queryHistory` select an available store node for you.
|
|||||||
However, it can only select a connected node, which is why the bootstrapping is necessary.
|
However, it can only select a connected node, which is why the bootstrapping is necessary.
|
||||||
It will throw an error if no store node is available.
|
It will throw an error if no store node is available.
|
||||||
|
|
||||||
## Filter messages
|
## Filter messages by send time
|
||||||
|
|
||||||
By default, Waku Store nodes store messages for 30 days.
|
By default, Waku Store nodes store messages for 30 days.
|
||||||
Depending on your use case, you may not need to retrieve 30 days worth of messages.
|
Depending on your use case, you may not need to retrieve 30 days worth of messages.
|
||||||
|
|
||||||
Messages have an optional unencrypted `timestamp` field.
|
[Waku Message](https://rfc.vac.dev/spec/14/) defines an optional unencrypted `timestamp` field.
|
||||||
The timestamp is set by the sender and may be present.
|
The timestamp is set by the sender.
|
||||||
By default, js-waku [sets the timestamp of outgoing message to the current time](https://github.com/status-im/js-waku/blob/a056227538f9409aa9134c7ef0df25f602dbea58/src/lib/waku_message/index.ts#L76).
|
By default, js-waku [sets the timestamp of outgoing message to the current time](https://github.com/status-im/js-waku/blob/a056227538f9409aa9134c7ef0df25f602dbea58/src/lib/waku_message/index.ts#L76).
|
||||||
|
|
||||||
You can filter messages that include a timestamp with the `timeFilter` option.
|
You can filter messages that include a timestamp within given bound with the `timeFilter` option.
|
||||||
|
|
||||||
Let's only retrieve messages up to a week old:
|
Let's only retrieve messages up to a week old:
|
||||||
|
|
||||||
|
@ -147,19 +147,19 @@ To process messages as soon as they received (page by page), use the `callback`
|
|||||||
```js
|
```js
|
||||||
const ContentTopic = '/store-guide/1/news/proto';
|
const ContentTopic = '/store-guide/1/news/proto';
|
||||||
|
|
||||||
waku.store
|
const callback = (retrievedMessages) => {
|
||||||
.queryHistory([ContentTopic], {
|
const articles = retrievedMessages
|
||||||
callback: (retrievedMessages) => {
|
.map(decodeWakuMessage) // Decode messages
|
||||||
const articles = retrievedMessages
|
.filter(Boolean); // Filter out undefined values
|
||||||
.map(decodeWakuMessage) // Decode messages
|
|
||||||
.filter(Boolean); // Filter out undefined values
|
|
||||||
|
|
||||||
console.log(`${articles.length} articles have been retrieved`);
|
console.log(`${articles.length} articles have been retrieved`);
|
||||||
}
|
};
|
||||||
})
|
|
||||||
|
waku.store
|
||||||
|
.queryHistory([ContentTopic], { callback })
|
||||||
.catch((e) => {
|
.catch((e) => {
|
||||||
// Be sure to catch any potential error
|
// Catch any potential error
|
||||||
console.log('Failed to retrieve messages', e);
|
console.log('Failed to retrieve messages from store', e);
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -167,3 +167,36 @@ Note that `WakuStore.queryHistory` select an available store node for you.
|
|||||||
However, it can only select a connected node, which is why the bootstrapping is necessary.
|
However, it can only select a connected node, which is why the bootstrapping is necessary.
|
||||||
It will throw an error if no store node is available.
|
It will throw an error if no store node is available.
|
||||||
|
|
||||||
|
## Filter messages by send time
|
||||||
|
|
||||||
|
By default, Waku Store nodes store messages for 30 days.
|
||||||
|
Depending on your use case, you may not need to retrieve 30 days worth of messages.
|
||||||
|
|
||||||
|
[Waku Message](https://rfc.vac.dev/spec/14/) defiles an optional unencrypted `timestamp` field.
|
||||||
|
The timestamp is set by the sender.
|
||||||
|
By default, js-waku [sets the timestamp of outgoing message to the current time](https://github.com/status-im/js-waku/blob/a056227538f9409aa9134c7ef0df25f602dbea58/src/lib/waku_message/index.ts#L76).
|
||||||
|
|
||||||
|
You can filter messages that include a timestamp within given bound with the `timeFilter` option.
|
||||||
|
|
||||||
|
Let's only retrieve messages up to a week old:
|
||||||
|
|
||||||
|
```js
|
||||||
|
// `ContentTopic` and `callback` definitions
|
||||||
|
|
||||||
|
const startTime = new Date();
|
||||||
|
// 7 days/week, 24 hours/day, 60min/hour, 60secs/min, 100ms/sec
|
||||||
|
startTime.setTime(startTime.getTime() - 7 * 24 * 60 * 60 * 1000);
|
||||||
|
|
||||||
|
waku.store
|
||||||
|
.queryHistory([ContentTopic], {
|
||||||
|
callback,
|
||||||
|
timeFilter: { startTime, endTime: new Date() }
|
||||||
|
})
|
||||||
|
.catch((e) => {
|
||||||
|
console.log('Failed to retrieve messages from store', e);
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
## End result
|
||||||
|
|
||||||
|
You can see a similar example implemented in ReactJS in the [Minimal ReactJS Waku Store App](/examples/store-reactjs-chat).
|
||||||
|
Loading…
x
Reference in New Issue
Block a user