js.waku.guide/content/docs/guides/06_light_push_send_messages.md
2022-01-31 10:39:20 +11:00

3.1 KiB

title date weight
Send Messages Using Waku Light Push 2021-12-09T14:00:00+01:00 6

Send Messages Using Waku Light Push

Waku Light Push enables a client to receive a confirmation when sending a message.

The Waku Relay protocol sends messages to connected peers but does not provide any information on whether said peers have received messages. This can be an issue when facing potential connectivity issues. For example, when the connection drops easily, or it is connected to a small number of relay peers.

Waku Light Push allows a client to get a response from a remote peer when sending a message. Note this only guarantees that the remote peer has received the message, it cannot guarantee propagation to the network.

It also means weaker privacy properties as the remote peer knows the client is the originator of the message. Whereas with Waku Relay, a remote peer would not know whether the client created or forwarded the message.

You can find Waku Light Push's specifications on Vac RFC.

Content Topic

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

For this guide, we are using a single content topic: /light-push-guide/1/guide/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({ bootstrap: { default: true } });

Passing the bootstrap option will connect your node to predefined Waku nodes. If you want to bootstrap to your own nodes, you can pass an array of multiaddresses instead:

import {Waku} from "js-waku";

const waku = await Waku.create({
    bootstrap: {
        peers: [
            "/dns4/node-01.ac-cn-hongkong-c.wakuv2.test.statusim.net/tcp/443/wss/p2p/16Uiu2HAkvWiyFsgRhuJEb9JfjYxEkoHLgnUQmr1N5mKWnYjxYRVm",
            "/dns4/node-01.do-ams3.wakuv2.test.statusim.net/tcp/443/wss/p2p/16Uiu2HAmPLe7Mzm8TsYUubgCAW1aJoeFScxrLj8ppHFivPo97bUZ",
        ]
    },
});

Wait to be connected

When using the bootstrap option, it may take some time to connect to other peers. To ensure that you have a light push peer available to send messages to, use the following function:

await waku.waitForConnectedPeer();

The returned Promise will resolve once you are connected to a Waku peer.

Send messages

You can now send a message using Waku Light Push. By default, it sends the messages to a single randomly selected light push peer. The peer is selected among the dApp's connected peers.

If the dApp is not connected to any light push peer, an error is thrown.

import { WakuMessage } from "js-waku";

const wakuMessage = await WakuMessage.fromUtf8String(
  "Here is a message",
  `/light-push-guide/1/guide/proto`
);

const ack = await waku.lightPush.push(wakuMessage);
if (!ack?.isSuccess) {
  // Message was not sent
}