Skip to content

Commit 0db30ef

Browse files
authored
Merge pull request #313 from status-im/unpkg-example
2 parents 6823b98 + 137c1df commit 0db30ef

File tree

5 files changed

+130
-2
lines changed

5 files changed

+130
-2
lines changed

.cspell.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"ipfs",
4141
"iwant",
4242
"jdev",
43+
"jswaku",
4344
"keccak",
4445
"lastpub",
4546
"libauth",

README.md

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,42 @@ Install `js-waku` package:
3232
npm install js-waku
3333
```
3434

35-
### Start a waku node
35+
### Import js-waku
3636

37-
```ts
37+
To use js-waku in your application, you can:
38+
39+
use `import`:
40+
41+
```js
3842
import { Waku } from 'js-waku';
3943

44+
const waku = await Waku.create();
45+
```
46+
47+
use `require`:
48+
49+
```js
50+
const jsWaku = require('js-waku');
51+
52+
jsWaku.Waku.create().then(waku => {
53+
// ...
54+
});
55+
```
56+
57+
Or directly import it in a `<script>` tag:
58+
59+
```html
60+
<script src='https://unpkg.com/[email protected]/build/umd/js-waku.min.bundle.js'></script>
61+
<script>
62+
jswaku.Waku.create().then(waku => {
63+
// ...
64+
}
65+
</script>
66+
```
67+
68+
### Start a waku node
69+
70+
```ts
4071
const waku = await Waku.create({ bootstrap: true });
4172
```
4273

examples/examples.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ Here is the list of the code examples and the features they demonstrate:
66
- [Ethereum Private Message Web App](eth-pm): Private Messaging, React/TypeScript, Light Push, Signature with Web3, Asymmetric Encryption.
77
- [Minimal ReactJS Chat App](min-react-js-chat): Group chat, React/JavaScript, Relay, Protobuf using `protons`.
88
- [Minimal ReactJS Waku Store App](store-reactjs-chat): Waku Store, React/JavaScript, Protobuf using `protons`.
9+
- [Pure Javascript Using Minified Library](unpkg-js-store): Stop retrieving results from Waku Store on condition, Use minified bundle from Unpkg.com, JavaScript.

examples/unpkg-js-store/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Pure Javascript Using Minified Library
2+
3+
**Demonstrates**:
4+
5+
- Waku Store: Using a condition to stop retrieving results from Waku Store.
6+
- Pure Javascript/HTML.
7+
- Use minified bundle of js from unpkg.com, no import, no package manager.
8+
9+
This example uses Waku Store to retrieve the latest ping relay message (used for keep alive purposes) and displays its timestamp.
10+
11+
To test the example, simply download the `index.html` file from this folder and open it in a browser.

examples/unpkg-js-store/index.html

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<!DOCTYPE html>
2+
<html lang='en'>
3+
4+
<head>
5+
<meta charset='UTF-8' />
6+
<meta content='width=device-width, initial-scale=1.0' name='viewport' />
7+
<title>JS-Waku unpkg example</title>
8+
</head>
9+
10+
<body>
11+
12+
<div><h1>Timestamp of latest relay ping</h1></div>
13+
<div id='timestamp'></div>
14+
15+
<script
16+
src='https://unpkg.com/[email protected]/build/umd/js-waku.min.bundle.js'></script>
17+
<script>
18+
/**
19+
* This example demonstrates how to use the js-waku minified bundle
20+
* available on unpkg.com.
21+
*
22+
* It is a simple scripts that uses Waku Store to retrieve ping relay messages
23+
* and displays the timestamp of the most recent ping relay message.
24+
*
25+
* More explanations are provided in this guide (TODO).
26+
*/
27+
const timestampDiv = document.getElementById('timestamp');
28+
29+
try {
30+
timestampDiv.innerHTML = '<p>Starting waku.</p>';
31+
jswaku.Waku.create({ bootstrap: true }).catch(e => {
32+
timestampDiv.innerHTML = 'Failed to create Waku: ' + e.toString();
33+
}
34+
).then(waku => {
35+
timestampDiv.innerHTML = '<p>Connecting to a peer.</p>';
36+
waku.waitForConnectedPeer()
37+
.catch((e) => {
38+
timestampDiv.innerHTML = 'Failed to connect to peers' + e.toString();
39+
})
40+
.then(() => {
41+
timestampDiv.innerHTML = '<p>Retrieving messages.</p>';
42+
43+
const callback = (wakuMessages) => {
44+
// Messages are ordered with oldest first
45+
// even with page direction `backward`
46+
const latestFirst = wakuMessages.reverse();
47+
const latestMessage = latestFirst[0];
48+
if (latestMessage) {
49+
timestampDiv.innerHTML = latestMessage.timestamp;
50+
} else {
51+
timestampDiv.innerHTML = '<p>No ping message available</p>';
52+
}
53+
54+
// When returning true, `queryHistory` stops retrieving pages
55+
// In our case, we only want one message, hence one page.
56+
return true;
57+
};
58+
59+
const startTime = new Date();
60+
// Only retrieve a week of messages
61+
startTime.setTime(Date.now() - 7 * 24 * 60 * 60 * 1000);
62+
63+
waku.store
64+
.queryHistory(['/relay-ping/1/ping/null'], {
65+
callback,
66+
pageDirection: 'backward',
67+
pageSize: 1,
68+
timeFilter: {
69+
startTime,
70+
endTime: new Date()
71+
}
72+
})
73+
.catch((e) => {
74+
timestampDiv.innerHTML = 'Failed to retrieve messages' + e.toString();
75+
});
76+
});
77+
});
78+
} catch (e) {
79+
timestampDiv.innerHTML = 'Failed to start application' + e.toString();
80+
}
81+
</script>
82+
</body>
83+
84+
</html>

0 commit comments

Comments
 (0)