Skip to content

Commit

Permalink
feat: use wakuv2test nodes for bootstrapping
Browse files Browse the repository at this point in the history
  • Loading branch information
adklempner committed May 16, 2024
1 parent 2c99b55 commit 6f478dd
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 29 deletions.
1 change: 1 addition & 0 deletions examples/sent-received-ratio/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/sent-received-ratio/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"start": "webpack-dev-server"
},
"dependencies": {
"@waku/discovery": "^0.0.3-e49e728.0",
"@waku/sdk": "0.0.26-e49e728.0"
},
"devDependencies": {
Expand Down
92 changes: 63 additions & 29 deletions examples/sent-received-ratio/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,74 @@ import {
DecodedMessage,
bytesToUtf8,
waitForRemotePeer,
Protocols,
DefaultPubsubTopic,
LightNode,
} from "@waku/sdk";

import {
wakuDnsDiscovery,
enrTree,
wakuLocalPeerCacheDiscovery,
wakuPeerExchangeDiscovery,
} from "@waku/discovery";
import { bootstrap } from "@libp2p/bootstrap";

const DEFAULT_CONTENT_TOPIC = "/js-waku-examples/1/message-ratio/utf8";

export async function sender() {
const node = await createLightNode({
contentTopics: [DEFAULT_CONTENT_TOPIC],
defaultBootstrap: true,
const testPeers = [
"/dns4/node-01.do-ams3.wakuv2.test.status.im/tcp/30303/p2p/16Uiu2HAmPLe7Mzm8TsYUubgCAW1aJoeFScxrLj8ppHFivPo97bUZ",
"/dns4/node-01.gc-us-central1-a.wakuv2.test.status.im/tcp/30303/p2p/16Uiu2HAmJb2e28qLXxT5kZxVUUoJt72EMzNGXB47Rxx5hw3q4YjS",
"/dns4/node-01.ac-cn-hongkong-c.wakuv2.test.status.im/tcp/30303/p2p/16Uiu2HAkvWiyFsgRhuJEb9JfjYxEkoHLgnUQmr1N5mKWnYjxYRVm",
];

const wakuNode = async (): Promise<LightNode> => {
return await createLightNode({
pubsubTopics: [DefaultPubsubTopic],
bootstrapPeers: testPeers,
libp2p: {
peerDiscovery: [
wakuDnsDiscovery([enrTree["SANDBOX"]]),
wakuLocalPeerCacheDiscovery(),
wakuPeerExchangeDiscovery([DefaultPubsubTopic]),
bootstrap({ list: testPeers }),
],
},
});
};

export async function sender() {
const node = await wakuNode();
await node.start();
await waitForRemotePeer(node);
const encoder = createEncoder({ contentTopic: DEFAULT_CONTENT_TOPIC });
const encoder = createEncoder({
pubsubTopic: DefaultPubsubTopic,
contentTopic: DEFAULT_CONTENT_TOPIC,
});

const start = (numMessages: number, period: number = 3000) => {
let intervalId: NodeJS.Timeout;
let i = 0;
const stream = new ReadableStream({
start(controller) {
intervalId = setInterval(async () => {
const result = await node.lightPush.send(encoder, {
payload: utf8ToBytes(String(i)),
timestamp: new Date(),
});
controller.enqueue({ result, i });
i++;
if (i >= numMessages) {
clearInterval(intervalId);
controller.close();
try {
const result = await node.lightPush.send(encoder, {
payload: utf8ToBytes(String(i)),
timestamp: new Date(),
});
if (result.successes.length > 0) {
controller.enqueue({ result, i });
i++;
}
if (result.failures.length > 0) {
console.error("Failed to send message", result.failures);
}
if (i >= numMessages) {
clearInterval(intervalId);
controller.close();
}
} catch (error) {
console.error("Error sending message", error);
}
}, period);
},
Expand All @@ -56,27 +95,24 @@ export async function sender() {
}

export async function receiver(callback: (message: DecodedMessage) => void) {
const node = await createLightNode({
contentTopics: [DEFAULT_CONTENT_TOPIC],
defaultBootstrap: true,
});
const node = await wakuNode();
await node.start();

await waitForRemotePeer(node);

const decoder = createDecoder(DEFAULT_CONTENT_TOPIC);
const decoder = createDecoder(DEFAULT_CONTENT_TOPIC, DefaultPubsubTopic);

const subscription = await node.filter.subscribe(decoder, callback);

return { node, subscription };
}

let messages = [];
let messages = [];

const startReceiver = async () => {
const messagesReceived = document.getElementById("messagesReceived");
const subscriptionCallback = (message: DecodedMessage) => {
const messageValue = bytesToUtf8(message.payload)
const messageValue = bytesToUtf8(message.payload);
messages.push(messageValue);
const messageElement = document.createElement("div");
messageElement.textContent = `Message: ${messageValue}`;
Expand All @@ -90,16 +126,12 @@ const startReceiver = async () => {
(window as any).receiver = node;
};

const startSender = async (numMessages: number) => {
const startSender = async (numMessages: number, messagePeriod: number) => {
const { node, start } = await sender();
(window as any).sender = node;
console.log("Sender started.");

document.getElementById("senderStatusText").textContent = "Running Sender";
const messagePeriod = parseInt(
(document.getElementById("messagePeriod") as HTMLInputElement).value,
3
);
const { stream, stop } = start(numMessages, messagePeriod * 1000);
const reader = stream.getReader();
const intervalId = setInterval(() => {
Expand All @@ -118,14 +150,16 @@ const startSender = async (numMessages: number) => {
}, messagePeriod * 1000 + 1);
};


document.getElementById("start").addEventListener("click", async () => {
const startButton = document.getElementById("start") as HTMLButtonElement;
startButton.disabled = true;
const numMessages = parseInt(
(document.getElementById("numMessages") as HTMLInputElement).value,
10
);
const messagePeriod = parseInt(
(document.getElementById("messagePeriod") as HTMLInputElement).value
);
await startReceiver();
await startSender(numMessages);
await startSender(numMessages, messagePeriod);
});

0 comments on commit 6f478dd

Please sign in to comment.