js-waku/guides/relay-receive-send-messages.md
2021-08-02 10:45:09 +10:00

3.0 KiB

Receive and Send messages using Waku Relay

Waku Relay is a gossip protocol that enables you to send and receive messages. You can find Waku Relay's specifications on Vac RFC.

Before starting, you need to choose a Content Topic for your dApp. Check out the choose a content topic guide to learn more about content topics.

For the purpose of this guide, we are using a unique content topic: /relay-guide/1/chat/proto.

Installation

You can install js-waku using your favorite package manager:

npm install js-waku

Create Waku instance

In order to interact with the Waku network, you first need a Waku instance:

import { Waku } from 'js-waku';

const wakuNode = await Waku.create();

Connect to other peers

The Waku instance needs to connect to other peers to communicate with the network. You are free to choose any method to bootstrap and DappConnect will ship with new methods in the future.

For now, the easiest way is to connect to Status' Waku fleet:

import { getStatusFleetNodes } from 'js-waku';

const nodes = await getStatusFleetNodes();
await Promise.all(nodes.map((addr) => waku.dial(addr))); 

Receive messages

To monitor messages for your app, you need to register an observer on relay for your app's content topic:

const processIncomingMessage = (wakuMessage) => {
  console.log("Message Received", wakuMessage);
};

waku.relay.addObserver(processIncomingMessage, ["/relay-guide/1/chat/proto"]);

Send messages

You are now ready to send messages. Let's start by sending simple strings as messages.

To send a message, you need to wrap the message in a WakuMessage. When using a basic string payload, you can use the WakuMessage.fromUtf8String helper:

import { WakuMessage } from 'js-waku';

const wakuMessage = await WakuMessage.fromUtf8String(message, `/relay-guide/1/chat/proto`);

Then, use the relay module to send the message to our peers, the message will then be relayed to the rest of the network thanks to Waku Relay:

import { WakuMessage } from 'js-waku';

const wakuMessage = await WakuMessage.fromUtf8String(message, `/relay-guide/1/chat/proto`);

await waku.relay.send(wakuMessage);

Conclusion

That is it! Now, you know how to send and receive messages over Waku using the Waku Relay protocol.

Feel free to check out other guides or examples.

Here is the final code:

import { getStatusFleetNodes, Waku, WakuMessage } from 'js-waku';

const wakuNode = await Waku.create();

const nodes = await getStatusFleetNodes();
await Promise.all(nodes.map((addr) => waku.dial(addr)));

const processIncomingMessage = (wakuMessage) => {
  console.log(`Message Received: ${wakuMessage.payloadAsUtf8}`);
};

waku.relay.addObserver(processIncomingMessage, ['/relay-guide/1/chat/proto']);

const wakuMessage = await WakuMessage.fromUtf8String(message, `/relay-guide/1/chat/proto`);
await waku.relay.send(wakuMessage);