2021-05-13 06:49:29 +00:00
# js-waku
2021-03-04 22:34:01 +00:00
2021-05-12 03:18:37 +00:00
A JavaScript implementation of the [Waku v2 protocol ](https://rfc.vac.dev/spec/10/ ).
2021-03-15 03:35:10 +00:00
2021-05-12 03:18:37 +00:00
## Usage
2021-05-10 06:52:53 +00:00
2021-05-13 06:49:29 +00:00
Install `js-waku` package:
2021-05-12 03:18:37 +00:00
```shell
2021-05-13 06:49:29 +00:00
npm install js-waku
2021-05-12 03:18:37 +00:00
```
Start a waku node:
```javascript
2021-05-13 06:49:29 +00:00
import { Waku } from 'js-waku';
2021-05-12 03:18:37 +00:00
const waku = await Waku.create();
```
Connect to a new peer:
```javascript
import { multiaddr } from 'multiaddr';
import PeerId from 'peer-id';
2021-05-13 01:51:39 +00:00
// Directly dial a new peer
2021-05-14 03:56:00 +00:00
await waku.dial('/dns4/node-01.do-ams3.wakuv2.test.statusim.net/tcp/443/wss/p2p/16Uiu2HAmPLe7Mzm8TsYUubgCAW1aJoeFScxrLj8ppHFivPo97bUZ');
2021-05-13 01:51:39 +00:00
// Or, add peer to address book so it auto dials in the background
2021-05-13 01:47:03 +00:00
waku.addPeerToAddressBook(
'16Uiu2HAmPLe7Mzm8TsYUubgCAW1aJoeFScxrLj8ppHFivPo97bUZ',
2021-05-14 03:56:00 +00:00
['/dns4/node-01.do-ams3.wakuv2.test.statusim.net/tcp/443/wss']
2021-05-12 03:18:37 +00:00
);
```
2021-05-15 10:42:54 +00:00
You can also use `getStatusFleetNodes` to connect to nodes run by Status:
```javascript
import { getStatusFleetNodes } from 'js-waku';
const nodes = await getStatusFleetNodes();
await Promise.all(
nodes.map((addr) => {
return waku.dial(addr);
})
);
```
2021-05-12 03:18:37 +00:00
The `contentTopic` is a metadata `string` that allows categorization of messages on the waku network.
2021-05-14 05:58:09 +00:00
Depending on your use case, you can either create one (or several) new `contentTopic` (s) or look at the [RFCs ](https://rfc.vac.dev/ ) and use an existing `contentTopic` .
2021-06-01 06:07:44 +00:00
See the [Waku v2 Topic Usage Recommendations ](https://rfc.vac.dev/spec/23/ ) for more details.
2021-05-12 03:18:37 +00:00
2021-06-01 06:07:44 +00:00
For example, if you were to use a new `contentTopic` such as `/my-cool-app/1/my-use-case/proto` ,
2021-05-14 05:58:09 +00:00
here is how to listen to new messages received via [Waku v2 Relay ](https://rfc.vac.dev/spec/11/ ):
2021-05-12 03:18:37 +00:00
```javascript
waku.relay.addObserver((msg) => {
console.log("Message received:", msg.payloadAsUtf8)
2021-06-01 06:07:44 +00:00
}, ["/my-cool-app/1/my-use-case/proto"]);
2021-05-12 03:18:37 +00:00
```
2021-06-01 05:27:14 +00:00
The examples chat apps currently use content topic `"/toy-chat/2/huilong/proto"` .
2021-05-14 05:58:09 +00:00
2021-05-12 03:18:37 +00:00
Send a message on the waku relay network:
```javascript
2021-05-13 06:49:29 +00:00
import { WakuMessage } from 'js-waku';
2021-05-12 03:18:37 +00:00
2021-07-15 05:02:16 +00:00
const msg = await WakuMessage.fromUtf8String("Here is a message!", { contentTopic: "/my-cool-app/1/my-use-case/proto" })
2021-05-12 03:18:37 +00:00
await waku.relay.send(msg);
```
The [Waku v2 Store protocol ](https://rfc.vac.dev/spec/13/ ) enables full nodes to store messages received via relay
and clients to retrieve them (e.g. after resuming connectivity).
The protocol implements pagination meaning that it may take several queries to retrieve all messages.
Query a waku store peer to check historical messages:
```javascript
// Process messages once they are all retrieved:
2021-07-15 05:02:16 +00:00
const messages = await waku.store.queryHistory({ contentTopics: ["my-cool-app"] });
2021-05-12 03:18:37 +00:00
messages.forEach((msg) => {
console.log("Message retrieved:", msg.payloadAsUtf8)
})
// Or, pass a callback function to be executed as pages are received:
2021-07-15 05:02:16 +00:00
waku.store.queryHistory({
contentTopics: ["my-cool-app"],
callback: (messages) => {
messages.forEach((msg) => {
console.log("Message retrieved:", msg.payloadAsUtf8);
});
}
2021-05-12 03:18:37 +00:00
});
```
Find more [examples ](#examples ) below
2021-05-13 06:49:29 +00:00
or checkout the latest `main` branch documentation at [https://status-im.github.io/js-waku/docs/ ](https://status-im.github.io/js-waku/docs/ ).
2021-05-10 06:52:53 +00:00
Docs can also be generated locally using:
```shell
npm install
npm run doc
```
2021-05-15 10:59:52 +00:00
## Changelog
Release changelog can be found [here ](https://github.com/status-im/js-waku/blob/main/CHANGELOG.md ).
2021-04-09 01:55:41 +00:00
## Waku Protocol Support
2021-05-13 06:49:29 +00:00
You can track progress on the [project board ](https://github.com/status-im/js-waku/projects/1 ).
2021-04-13 00:56:13 +00:00
2021-04-09 01:55:41 +00:00
- ✔: Supported
- 🚧: Implementation in progress
- ⛔: Support is not planned
| Spec | Implementation Status |
| ---- | -------------- |
|[6/WAKU1](https://rfc.vac.dev/spec/6)|⛔|
|[7/WAKU-DATA](https://rfc.vac.dev/spec/7)|⛔|
|[8/WAKU-MAIL](https://rfc.vac.dev/spec/8)|⛔|
|[9/WAKU-RPC](https://rfc.vac.dev/spec/9)|⛔|
|[10/WAKU2](https://rfc.vac.dev/spec/10)|🚧|
|[11/WAKU2-RELAY](https://rfc.vac.dev/spec/11)|✔|
|[12/WAKU2-FILTER](https://rfc.vac.dev/spec/12)||
2021-04-20 00:24:03 +00:00
|[13/WAKU2-STORE](https://rfc.vac.dev/spec/13)|✔ (querying node only)|
2021-04-09 01:55:41 +00:00
|[14/WAKU2-MESSAGE](https://rfc.vac.dev/spec/14)|✔|
|[15/WAKU2-BRIDGE](https://rfc.vac.dev/spec/15)||
|[16/WAKU2-RPC](https://rfc.vac.dev/spec/16)|⛔|
|[17/WAKU2-RLNRELAY](https://rfc.vac.dev/spec/17)||
|[18/WAKU2-SWAP](https://rfc.vac.dev/spec/18)||
2021-05-19 01:00:43 +00:00
|[19/WAKU2-LIGHTPUSH](https://rfc.vac.dev/spec/19/)|✔|
2021-04-09 01:55:41 +00:00
2021-04-13 00:56:13 +00:00
## Bugs, Questions & Features
2021-04-01 00:09:51 +00:00
2021-05-13 06:49:29 +00:00
If you encounter any bug or would like to propose new features, feel free to [open an issue ](https://github.com/status-im/js-waku/issues/new/ ).
2021-04-13 00:56:13 +00:00
2021-05-15 10:41:05 +00:00
For support, questions & more general topics,
please join the discussion on the [Vac forum ](https://forum.vac.dev/tag/js-waku ) (use _\#js-waku_ tag)
2021-06-08 11:12:46 +00:00
or [\#waku-support on Status Community Discord ](https://discord.gg/Q9aNS6Fg ).
2021-04-01 00:09:51 +00:00
## Examples
2021-04-27 23:53:33 +00:00
## Web Chat App (ReactJS)
2021-05-12 03:18:37 +00:00
A ReactJS chat app is provided as a showcase of the library used in the browser.
2021-06-01 06:10:03 +00:00
It implements [Waku v2 Toy Chat ](https://rfc.vac.dev/spec/22/ ) protocol.
A deployed version is available at https://status-im.github.io/js-waku/.
2021-04-27 23:53:33 +00:00
2021-05-13 06:49:29 +00:00
Find the code in the [examples folder ](https://github.com/status-im/js-waku/tree/main/examples/web-chat ).
2021-05-12 03:18:37 +00:00
2021-04-27 23:53:33 +00:00
To run a development version locally, do:
```shell
2021-05-13 06:49:29 +00:00
git clone https://github.com/status-im/js-waku/ ; cd js-waku
npm install # Install dependencies for js-waku
npm run build # Build js-waku
2021-05-05 04:57:46 +00:00
cd examples/web-chat
2021-05-03 06:34:52 +00:00
npm install # Install dependencies for the web app
2021-05-13 06:49:29 +00:00
npm run start # Start development server to serve the web app on http://localhost:3000/js-waku
2021-04-27 23:53:33 +00:00
```
2021-05-03 06:34:52 +00:00
Use `/help` to see the available commands.
2021-04-27 23:53:33 +00:00
## CLI Chat App (NodeJS)
2021-04-01 00:09:51 +00:00
A node chat app is provided as a working example of the library.
2021-06-01 06:10:03 +00:00
It implements [Waku v2 Toy Chat ](https://rfc.vac.dev/spec/22/ ) protocol.
2021-05-12 03:18:37 +00:00
2021-05-13 06:49:29 +00:00
Find the code in the [examples folder ](https://github.com/status-im/js-waku/tree/main/examples/cli-chat ).
2021-05-12 03:18:37 +00:00
2021-04-23 06:33:53 +00:00
To run the chat app, first ensure you have [Node.js ](https://nodejs.org/en/ ) v14 or above:
```shell
node --version
```
Then, install and run:
2021-04-01 00:09:51 +00:00
```shell
2021-05-13 06:49:29 +00:00
git clone https://github.com/status-im/js-waku/ ; cd js-waku
npm install # Install dependencies for js-waku
npm run build # Build js-waku
2021-05-04 05:12:36 +00:00
cd examples/cli-chat
npm install # Install dependencies for the cli app
2021-05-14 03:23:44 +00:00
npm run start -- --autoDial
2021-04-01 00:09:51 +00:00
```
2021-04-23 06:33:53 +00:00
You can also specify an optional `listenAddr` parameter (.e.g `--listenAddr /ip4/0.0.0.0/tcp/7777/ws` ).
2021-04-01 04:46:14 +00:00
This is only useful if you want a remote node to dial to your chat app,
it is not necessary in normal usage when you just connect to the fleet.
2021-03-15 05:22:26 +00:00
2021-06-21 05:48:56 +00:00
## Ethereum Direct Message
A PoC implementation of [20/ETH-DM ](https://rfc.vac.dev/spec/20/ ).
Ethereum Direct Message, or Eth-DM, is a protocol that allows sending encrypted message to a recipient,
only knowing their Ethereum Address.
This is protocol has been created to demonstrated how encryption and signature could be added to messages
sent over the Waku v2 network.
The `main` branch's HEAD is deployed on GitHub Pages at https://status-im.github.io/js-waku/eth-dm/.
To run a development version locally, do:
```shell
git clone https://github.com/status-im/js-waku/ ; cd js-waku
npm install # Install dependencies for js-waku
npm run build # Build js-waku
cd examples/eth-dm
npm install # Install dependencies for the web app
npm run start # Start development server to serve the web app on http://localhost:3000/js-waku/eth-dm
```
2021-03-15 05:22:26 +00:00
## Contributing
2021-04-29 05:22:49 +00:00
See [CONTRIBUTING.md ](./CONTRIBUTING.md ).
2021-04-28 06:29:43 +00:00
## License
Licensed and distributed under either of
* MIT license: [LICENSE-MIT ](LICENSE-MIT ) or http://opensource.org/licenses/MIT
or
* Apache License, Version 2.0, ([LICENSE-APACHE-v2](LICENSE-APACHE-v2) or http://www.apache.org/licenses/LICENSE-2.0)
at your option. These files may not be copied, modified, or distributed except according to those terms.