Skip to content

Commit

Permalink
Setup node to use autosharding in light-js
Browse files Browse the repository at this point in the history
  • Loading branch information
adklempner committed Jan 11, 2024
1 parent 504bcd4 commit 1cdcf14
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions examples/light-js/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,13 @@ <h2>Store</h2>
createDecoder,
utf8ToBytes,
bytesToUtf8,
} from "https://unpkg.com/@waku/sdk@0.0.20/bundle/index.js";
} from "https://unpkg.com/@waku/sdk@0.0.22/bundle/index.js";
import {
enrTree,
DnsNodeDiscovery,
} from "https://unpkg.com/@waku/dns-discovery@0.0.16/bundle/index.js";
import { messageHash } from "https://unpkg.com/@waku/message-hash@0.1.8/bundle/index.js";
} from "https://unpkg.com/@waku/dns-discovery@0.0.20/bundle/index.js";
import { messageHash } from "https://unpkg.com/@waku/message-hash@0.1.10/bundle/index.js";
import { contentTopicToShardIndex } from "https://unpkg.com/@waku/utils@0.0.14/bundle/index.js";

const peerIdDiv = document.getElementById("peer-id");
const remotePeerIdDiv = document.getElementById("remote-peer-id");
Expand All @@ -98,11 +99,19 @@ <h2>Store</h2>
const peersSelector = document.getElementById("peer-select");

const ContentTopic = "/js-waku-examples/1/chat/utf8";
const decoder = createDecoder(ContentTopic);
const encoder = createEncoder({ contentTopic: ContentTopic });
const shardInfo = {
clusterId: 1,
shard: contentTopicToShardIndex(ContentTopic),
};
console.log(shardInfo);
const decoder = createDecoder(ContentTopic, shardInfo);
const encoder = createEncoder({
contentTopic: ContentTopic,
pubsubTopicShardInfo: shardInfo,
});
// Each key is a unique identifier for the message. Each value is an obj { text, timestamp }
let messages = {};
let unsubscribe;
let subscription;

const updateMessages = (msgs, div) => {
div.innerHTML = "<ul>";
Expand All @@ -125,7 +134,9 @@ <h2>Store</h2>
}

statusDiv.innerHTML = "<p>Creating Waku node.</p>";
const node = await createLightNode();
const node = await createLightNode({
shardInfo: { clusterId: 1, contentTopics: [ContentTopic] },
});

statusDiv.innerHTML = "<p>Starting Waku node.</p>";
await node.start();
Expand Down Expand Up @@ -203,10 +214,9 @@ <h2>Store</h2>
};

subscribeButton.onclick = async () => {
unsubscribe = await node.filter.subscribe(
[decoder],
messageReceivedCallback
);
console.log(shardInfo);
const subscription = await node.filter.createSubscription(shardInfo);
subscription.subscribe(decoder, messageReceivedCallback);
unsubscribeButton.disabled = false;
subscribeButton.disabled = true;
};
Expand All @@ -219,7 +229,7 @@ <h2>Store</h2>
};

unsubscribeButton.onclick = async () => {
await unsubscribe();
await subscription.unsubscribe([ContentTopic]);
unsubscribe = undefined;
unsubscribeButton.disabled = true;
subscribeButton.disabled = false;
Expand Down

0 comments on commit 1cdcf14

Please sign in to comment.