* add @waku/react outline * add @waku/react hooks docs * add @waku/react providers docs * add more snippet comments + fix code indents
3.4 KiB
title |
---|
Send and Receive Messages Using Light Push and Filter |
This guide provides detailed steps to create a light node, send messages using the Light Push protocol, and receive messages using the Filter protocol.
Create a Light Node
Set up a Waku node by creating a light node, connecting to network peers with Light Push
and Filter
enabled, choosing a content topic, and creating an encoder
and decoder
for message encryption:
import {
createLightNode,
waitForRemotePeer,
Protocols,
createEncoder,
createDecoder,
} from "@waku/sdk";
// Create and start a light node
const node = await createLightNode({ defaultBootstrap: true });
await node.start();
// Wait for a successful peer connection
await waitForRemotePeer(node, [
Protocols.LightPush,
Protocols.Filter,
]);
// Choose a content topic
const contentTopic = "/light-guide/1/message/proto";
// Create a message encoder and decoder
const encoder = createEncoder({ contentTopic });
const decoder = createDecoder(contentTopic);
Create a Message Structure
Create your application's message structure using Protobuf's valid message fields:
import protobuf from "protobufjs";
// Create a message structure using Protobuf
const ChatMessage = new protobuf.Type("ChatMessage")
.add(new protobuf.Field("timestamp", 1, "uint64"))
.add(new protobuf.Field("sender", 2, "string"))
.add(new protobuf.Field("message", 3, "string"));
:::info
Please refer to the Protobuf installation guide for adding the protobufjs
package to your project.
:::
Send Messages Using Light Push
To send messages over the Waku Network using the Light Push
protocol, create a new message object and use the lightPush.send()
function:
// Create a new message object
const protoMessage = ChatMessage.create({
timestamp: Date.now(),
sender: "Alice",
message: "Hello, World!",
});
// Serialize the message using Protobuf
const serializedMessage = ChatMessage.encode(protoMessage).finish();
// Send the message using Light Push
await node.lightPush.send(encoder, {
payload: serializedMessage,
});
Receive Messages Using Filter
To receive messages using the Filter
protocol, create a callback function to process the messages and use the filter.subscribe()
function:
// Create the callback function
const callback = (wakuMessage) => {
// Check if there is a payload on the message
if (!wakuMessage.payload) return;
// Render the messageObj as desired in your application
const messageObj = ChatMessage.decode(wakuMessage.payload);
console.log(messageObj);
};
// Subscribe to content topics and display new messages
const unsubscribe = await node.filter.subscribe([decoder], callback);
// Use the unsubscribe() function to stop receiving messages
// await unsubscribe();
:::tip Congratulations!
You have successfully sent and received messages over the Waku Network using the Light Push
and Filter
protocols. Check out the light-js and light-chat examples for working demos.
:::