2023-07-11 18:52:38 +01:00
---
title: Send and Receive Messages Using Light Push and Filter
2023-11-23 12:01:32 +01:00
hide_table_of_contents: true
2025-09-29 16:34:46 +10:00
displayed_sidebar: build
2023-07-11 18:52:38 +01:00
---
2025-09-29 23:18:55 +10:00
This guide provides detailed steps to start using the `@waku/sdk` package by setting up a [Light Node ](/learn/glossary#light-node ) to send messages using the [Light Push protocol ](/learn/concepts/protocols#light-push ), and receive messages using the [Filter protocol ](/learn/concepts/protocols#filter ). Have a look at the [installation guide ](/build/javascript/#installation ) for steps on adding `@waku/sdk` to your project.
2023-07-11 18:52:38 +01:00
2023-11-23 12:01:32 +01:00
## Create a light node
2023-07-11 18:52:38 +01:00
2023-11-23 12:01:32 +01:00
Use the `createLightNode()` function to create a [Light Node ](/learn/glossary#light-node ) and interact with the Waku Network:
2023-07-11 18:52:38 +01:00
```js
2023-07-27 13:50:30 +01:00
import { createLightNode } from "@waku/sdk ";
// Create and start a Light Node
2023-07-11 18:52:38 +01:00
const node = await createLightNode({ defaultBootstrap: true });
await node.start();
2023-07-27 13:50:30 +01:00
// Use the stop() function to stop a running node
// await node.stop();
```
:::info
2025-09-29 23:18:55 +10:00
When the `defaultBootstrap` parameter is set to `true` , your node will be bootstrapped using the [default bootstrap method ](/build/javascript/configure-discovery#default-bootstrap-method ). Have a look at the [Bootstrap Nodes and Discover Peers ](/build/javascript/configure-discovery ) guide to learn more methods to bootstrap nodes.
2023-07-27 13:50:30 +01:00
:::
2024-10-01 07:53:34 +01:00
A node needs to know how to route messages. By default, it will use The Waku Network configuration (`{ clusterId: 1, shards: [0,1,2,3,4,5,6,7] }` ). For most applications, it's recommended to use autosharding:
```js
// Create node with auto sharding (recommended)
const node = await createLightNode({
defaultBootstrap: true,
networkConfig: {
clusterId: 1,
contentTopics: ["/my-app/1/notifications/proto"],
},
});
```
### Alternative network configuration
If your project requires a specific network configuration, you can use static sharding:
```js
// Create node with static sharding
const node = await createLightNode({
defaultBootstrap: true,
networkConfig: {
clusterId: 1,
shards: [0, 1, 2, 3],
},
});
```
2023-11-23 12:01:32 +01:00
## Connect to remote peers
2023-07-27 13:50:30 +01:00
2024-10-11 12:05:07 +02:00
Use the `node.waitForPeers()` function to wait for the node to connect with peers on the Waku Network:
2023-07-27 13:50:30 +01:00
```js
2023-07-11 18:52:38 +01:00
// Wait for a successful peer connection
2024-10-11 12:05:07 +02:00
await node.waitForPeers();
2023-07-27 13:50:30 +01:00
```
2024-03-08 05:06:21 +01:00
The `protocols` parameter allows you to specify the [protocols ](/learn/concepts/protocols ) that the remote peers should have enabled:
2023-07-27 13:50:30 +01:00
```js
2024-10-11 12:05:07 +02:00
import { Protocols } from "@waku/sdk ";
2023-07-27 13:50:30 +01:00
// Wait for peer connections with specific protocols
2024-10-11 12:05:07 +02:00
await node.waitForPeers([Protocols.LightPush, Protocols.Filter]);
2023-07-27 13:50:30 +01:00
```
2023-11-23 12:01:32 +01:00
## Choose a content topic
2023-07-27 13:50:30 +01:00
2024-10-01 07:53:34 +01:00
Choose a [content topic ](/learn/concepts/content-topics ) for your application and create a message `encoder` and `decoder` :
2023-07-27 13:50:30 +01:00
```js
import { createEncoder, createDecoder } from "@waku/sdk ";
2023-07-11 18:52:38 +01:00
// 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);
```
2025-09-29 23:18:55 +10:00
The `ephemeral` parameter allows you to specify whether messages should **NOT** be stored by [Store peers ](/build/javascript/store-retrieve-messages ):
2023-07-27 13:50:30 +01:00
```js
const encoder = createEncoder({
2024-10-01 07:53:34 +01:00
contentTopic: contentTopic, // message content topic
ephemeral: true, // allows messages NOT be stored on the network
2023-07-27 13:50:30 +01:00
});
```
2024-10-01 07:53:34 +01:00
The `pubsubTopicShardInfo` parameter allows you to configure a different network configuration for your `encoder` and `decoder` :
```js
// Create the network config
const networkConfig = { clusterId: 3, shards: [1, 2] };
// Create encoder and decoder with custom network config
const encoder = createEncoder({
contentTopic: contentTopic,
pubsubTopicShardInfo: networkConfig,
});
const decoder = createDecoder(contentTopic, networkConfig);
```
2023-07-27 13:50:30 +01:00
:::info
In this example, users send and receive messages on a shared content topic. However, real applications may have users broadcasting messages while others listen or only have 1:1 exchanges. Waku supports all these use cases.
:::
2023-11-23 12:01:32 +01:00
## Create a message structure
2023-07-11 18:52:38 +01:00
Create your application's message structure using [Protobuf's valid message ](https://github.com/protobufjs/protobuf.js#usage ) fields:
```js
import protobuf from "protobufjs";
// Create a message structure using Protobuf
2024-10-01 07:53:34 +01:00
const DataPacket = new protobuf.Type("DataPacket")
.add(new protobuf.Field("timestamp", 1, "uint64"))
.add(new protobuf.Field("sender", 2, "string"))
.add(new protobuf.Field("message", 3, "string"));
2023-07-11 18:52:38 +01:00
```
:::info
2025-09-29 23:18:55 +10:00
Have a look at the [Protobuf installation ](/build/javascript/#message-structure ) guide for adding the `protobufjs` package to your project.
2023-07-11 18:52:38 +01:00
:::
2023-11-23 12:01:32 +01:00
## Send messages using light push
2023-07-11 18:52:38 +01:00
To send messages over the Waku Network using the `Light Push` protocol, create a new message object and use the `lightPush.send()` function:
```js
// Create a new message object
2024-10-01 07:53:34 +01:00
const protoMessage = DataPacket.create({
timestamp: Date.now(),
sender: "Alice",
message: "Hello, World!",
2023-07-11 18:52:38 +01:00
});
2023-08-01 20:39:04 +01:00
// Serialise the message using Protobuf
2024-10-01 07:53:34 +01:00
const serialisedMessage = DataPacket.encode(protoMessage).finish();
2023-07-11 18:52:38 +01:00
// Send the message using Light Push
await node.lightPush.send(encoder, {
2024-10-01 07:53:34 +01:00
payload: serialisedMessage,
2023-07-11 18:52:38 +01:00
});
```
2023-11-23 12:01:32 +01:00
## Receive messages using filter
2023-07-11 18:52:38 +01:00
2023-07-27 13:50:30 +01:00
To receive messages using the `Filter` protocol, create a callback function for message processing, then use the `filter.subscribe()` function to subscribe to a `content topic` :
2023-07-11 18:52:38 +01:00
```js
2023-07-15 22:54:13 +01:00
// Create the callback function
const callback = (wakuMessage) => {
2024-10-01 07:53:34 +01:00
// Check if there is a payload on the message
if (!wakuMessage.payload) return;
// Render the messageObj as desired in your application
const messageObj = DataPacket.decode(wakuMessage.payload);
console.log(messageObj);
2023-07-15 22:54:13 +01:00
};
2024-01-11 10:42:56 +01:00
// Create a Filter subscription
2024-06-05 15:13:46 +02:00
const { error, subscription } = await node.filter.createSubscription({ contentTopics: [contentTopic] });
if (error) {
// handle errors if happens
throw Error(error);
}
2023-07-11 18:52:38 +01:00
2023-10-29 17:48:37 +01:00
// Subscribe to content topics and process new messages
await subscription.subscribe([decoder], callback);
```
2024-10-01 07:53:34 +01:00
The `pubsubTopicShardInfo` parameter allows you to configure a different network configuration for your `Filter` subscription:
```js
// Create the network config
const networkConfig = { clusterId: 3, shards: [1, 2] };
// Create Filter subscription with custom network config
const subscription = await node.filter.createSubscription(networkConfig);
```
2023-10-29 17:48:37 +01:00
You can use the `subscription.unsubscribe()` function to stop receiving messages from a content topic:
```js
await subscription.unsubscribe([contentTopic]);
2023-07-11 18:52:38 +01:00
```
:::tip Congratulations!
2023-08-01 20:39:04 +01:00
You have successfully sent and received messages over the Waku Network using the `Light Push` and `Filter` protocols. Have a look at the [light-js ](https://github.com/waku-org/js-waku-examples/tree/master/examples/light-js ) and [light-chat ](https://github.com/waku-org/js-waku-examples/tree/master/examples/light-chat ) examples for working demos.
2023-07-11 18:52:38 +01:00
:::