js.waku.guide/content/docs/quick_start.md

67 lines
1.6 KiB
Markdown
Raw Normal View History

---
title: Quick Start
date: 2021-12-09T14:00:00+01:00
2022-01-12 03:41:29 +00:00
weight: 20
---
2022-01-24 01:17:00 +00:00
2021-11-30 01:50:16 +00:00
# Quick Start
In this section you will learn how to receive and send messages using Waku Relay.
A more in depth guide for Waku Relay can be found [here](/docs/guides/02_relay_receive_send_messages/).
2021-11-30 01:50:16 +00:00
## Install
Install the `js-waku` package:
```shell
npm install js-waku
# or with yarn
yarn add js-waku
```
### Start a waku node
```ts
2022-01-24 01:17:00 +00:00
import { Waku } from "js-waku";
2021-11-30 01:50:16 +00:00
2022-01-24 01:17:00 +00:00
const waku = await Waku.create({ bootstrap: { default: true } });
2021-11-30 01:50:16 +00:00
```
### Listen for messages
The `contentTopic` is a metadata `string` that allows categorization of messages on the waku network.
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`.
See [How to Choose a Content Topic](/docs/guides/01_choose_content_topic/) for more details.
2021-11-30 01:50:16 +00:00
For example, if you were to use a new `contentTopic` such as `/my-cool-app/1/my-use-case/proto`,
here is how to listen to new messages received via [Waku v2 Relay](https://rfc.vac.dev/spec/11/):
```ts
2022-01-24 01:17:00 +00:00
waku.relay.addObserver(
(msg) => {
console.log("Message received:", msg.payloadAsUtf8);
},
["/my-cool-app/1/my-use-case/proto"]
);
2021-11-30 01:50:16 +00:00
```
### Send messages
Messages are wrapped in a `WakuMessage` envelop.
```ts
2022-01-24 01:17:00 +00:00
import { WakuMessage } from "js-waku";
2021-11-30 01:50:16 +00:00
2022-01-24 01:17:00 +00:00
const msg = await WakuMessage.fromUtf8String(
"Here is a message!",
"/my-cool-app/1/my-use-case/proto"
);
2021-11-30 01:50:16 +00:00
await waku.relay.send(msg);
```
### Building an app
2022-01-24 01:17:00 +00:00
Check out the [ReactJS Waku Relay guide](/docs/guides/07_reactjs_relay/) to learn how you can use the code above in a React app.