Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update shard info config for js-waku #193

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 53 additions & 19 deletions docs/guides/js-waku/light-send-receive.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ await node.start();
When the `defaultBootstrap` parameter is set to `true`, your node will be bootstrapped using the [default bootstrap method](/guides/js-waku/configure-discovery#default-bootstrap-method). Have a look at the [Bootstrap Nodes and Discover Peers](/guides/js-waku/configure-discovery) guide to learn more methods to bootstrap nodes.
:::

By default, your node will route messages to the standard pubsub topic (`/waku/2/default-waku/proto`). If your project uses a different shared pubsub topic, you can configure this using the `ShardInfo` parameter:
LordGhostX marked this conversation as resolved.
Show resolved Hide resolved

```js
// Create the shard info
const shardInfo = { clusterId: 3, shards: [1, 2] };

// Create node with custom shard info
const node = await createLightNode({
defaultBootstrap: true,
shardInfo: shardInfo,
LordGhostX marked this conversation as resolved.
Show resolved Hide resolved
});
```

## Connect to remote peers

Use the `waitForRemotePeer()` function to wait for the node to connect with peers on the Waku Network:
Expand All @@ -41,10 +54,7 @@ The `protocols` parameter allows you to specify the [protocols](/learn/concepts/
import { waitForRemotePeer, Protocols } from "@waku/sdk";

// Wait for peer connections with specific protocols
await waitForRemotePeer(node, [
Protocols.LightPush,
Protocols.Filter,
]);
await waitForRemotePeer(node, [Protocols.LightPush, Protocols.Filter]);
```

## Choose a content topic
Expand All @@ -66,11 +76,25 @@ The `ephemeral` parameter allows you to specify whether messages should **NOT**

```js
const encoder = createEncoder({
contentTopic: contentTopic, // message content topic
ephemeral: true, // allows messages NOT be stored on the network
contentTopic: contentTopic, // message content topic
ephemeral: true, // allows messages NOT be stored on the network
});
```

The `pubsubTopicShardInfo` parameter allows you to configure a different shared pubsub topic for your `encoder` and `decoder`:

```js
LordGhostX marked this conversation as resolved.
Show resolved Hide resolved
// Create the shard info
const shardInfo = { clusterId: 3, shard: 1 };

// Create encoder and decoder with custom shard info
const encoder = createEncoder({
contentTopic: contentTopic,
pubsubTopicShardInfo: shardInfo,
});
const decoder = createDecoder(contentTopic, shardInfo);
```

:::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.
:::
Expand All @@ -84,9 +108,9 @@ 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"));
.add(new protobuf.Field("timestamp", 1, "uint64"))
.add(new protobuf.Field("sender", 2, "string"))
.add(new protobuf.Field("message", 3, "string"));
```

:::info
Expand All @@ -100,17 +124,17 @@ To send messages over the Waku Network using the `Light Push` protocol, create a
```js
// Create a new message object
const protoMessage = ChatMessage.create({
LordGhostX marked this conversation as resolved.
Show resolved Hide resolved
timestamp: Date.now(),
sender: "Alice",
message: "Hello, World!",
timestamp: Date.now(),
sender: "Alice",
message: "Hello, World!",
});

// Serialise the message using Protobuf
const serialisedMessage = ChatMessage.encode(protoMessage).finish();

// Send the message using Light Push
await node.lightPush.send(encoder, {
payload: serialisedMessage,
payload: serialisedMessage,
});
```

Expand All @@ -121,11 +145,11 @@ To receive messages using the `Filter` protocol, create a callback function for
```js
// 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);
// 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);
};

// Create a Filter subscription
Expand All @@ -135,6 +159,16 @@ const subscription = await node.filter.createSubscription();
await subscription.subscribe([decoder], callback);
```

The `pubsubTopicShardInfo` parameter allows you to configure a different shared pubsub topic for your `Filter` subscription:

```js
// Create the shard info
const shardInfo = { clusterId: 3, shard: 1 };

// Create Filter subscription with custom shard info
const subscription = await node.filter.createSubscription(shardInfo);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a heads up that it will change soon as the current API seems to be too hard to understand. Maybe we can delay this PR or we can follow up on it. I think we will have it updated in a week or so

```

You can use the `subscription.unsubscribe()` function to stop receiving messages from a content topic:

```js
Expand All @@ -143,4 +177,4 @@ await subscription.unsubscribe([contentTopic]);

:::tip Congratulations!
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.
:::
:::
Loading