Skip to content

Commit

Permalink
feat: select peer to dial from drop down
Browse files Browse the repository at this point in the history
  • Loading branch information
adklempner committed Oct 30, 2023
1 parent 5f3b0e2 commit 6a11881
Showing 1 changed file with 94 additions and 15 deletions.
109 changes: 94 additions & 15 deletions examples/light-js/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,37 @@
</head>

<body>
<div><h2>Status</h2></div>
<div>
<h2>Status</h2>
</div>
<div id="status"></div>

<div><h2>Local Peer Id</h2></div>
<div>
<h2>Local Peer Id</h2>
</div>
<div id="peer-id"></div>

<div><h2>Remote Peer Id</h2></div>
<div id="remote-peer-id"></div>

<label for="remote-multiaddr">Remote peer's multiaddr</label>
<input id="remote-multiaddr" type="text" value="" />
<div>
<h2>Select Peer</h2>
</div>
<div>
<button id="getPeersButton" type="button">Refresh Peers</button>
<select id="peer-select">
<option value=""></option>
</select>
</div>
<div>
<label for="remote-multiaddr">Remote peer's multiaddr</label>
</div>
<div>
<input id="remote-multiaddr" type="text" value="" style="width: 100%" />
</div>
<button disabled id="dial" type="button">Dial</button>

<div>
<h2>Remote Peers</h2>
</div>
<div id="remote-peer-id"></div>
<br />
<button disabled id="subscribe" type="button">Subscribe with Filter</button>
<button disabled id="unsubscribe" type="button">
Expand Down Expand Up @@ -61,11 +80,15 @@
const messagesDiv = document.getElementById("messages");
const textInput = document.getElementById("textInput");
const sendButton = document.getElementById("sendButton");
const getPeersButton = document.getElementById("getPeersButton");
const peersSelector = document.getElementById("peer-select");

const ContentTopic = "/js-waku-examples/1/chat/utf8";
const decoder = createDecoder(ContentTopic);
const encoder = createEncoder({ contentTopic: ContentTopic });
let messages = [];
// Stores list of peers
let peers = [];
let unsubscribe;

const updateMessages = (msgs, div) => {
Expand All @@ -75,7 +98,7 @@
};

try {
await searchForPeer(statusDiv, remoteMultiAddrDiv);
await getPeers(statusDiv, remoteMultiAddrDiv);
} catch (e) {
console.log("Failed to find a peer", e);
remoteMultiAddrDiv.value =
Expand All @@ -87,6 +110,22 @@

statusDiv.innerHTML = "<p>Starting Waku node.</p>";
await node.start();
node.store.addLibp2pEventListener("peer:connect", (event) => {
const peerId = event.detail;
console.log(`peer:connect ${peerId}`);
setTimeout(() => {
node.store.peerStore.get(peerId).then((peerInfo) => {
console.log(peerInfo);
statusDiv.innerHTML = "<p>Peer dialed.</p>";
remotePeerIdDiv.appendChild(
document.createElement("p")
).textContent = `${peerInfo.addresses[1].multiaddr}/p2p/${peerId}`;
textInput.disabled = false;
sendButton.disabled = false;
subscribeButton.disabled = false;
});
}, 500);
});
statusDiv.innerHTML = "<p>Waku node started.</p>";
peerIdDiv.innerHTML = "<p>" + node.libp2p.peerId.toString() + "</p>";
dialButton.disabled = false;
Expand All @@ -100,13 +139,13 @@
statusDiv.innerHTML = "<p>Dialing peer.</p>";
const multiaddr = MultiformatsMultiaddr.multiaddr(ma);
await node.dial(multiaddr, ["filter", "lightpush"]);
await waitForRemotePeer(node, ["filter", "lightpush"]);
const peers = await node.libp2p.peerStore.all();
statusDiv.innerHTML = "<p>Peer dialed.</p>";
remotePeerIdDiv.innerHTML = "<p>" + peers[0].id.toString() + "</p>";
textInput.disabled = false;
sendButton.disabled = false;
subscribeButton.disabled = false;
// await waitForRemotePeer(node, ["filter", "lightpush"]);
// const peers = await node.libp2p.peerStore.all();
// statusDiv.innerHTML = "<p>Peer dialed.</p>";
// remotePeerIdDiv.innerHTML = "<p>" + peersSelector.value + "</p>";
// textInput.disabled = false;
// sendButton.disabled = false;
// subscribeButton.disabled = false;
};

const callback = (wakuMessage) => {
Expand Down Expand Up @@ -139,6 +178,46 @@
textInput.value = null;
};

getPeersButton.onclick = async () => {
await getPeers(statusDiv, remoteMultiAddrDiv);
};

peersSelector.addEventListener("change", function (event) {
remoteMultiAddrDiv.value = event.target.value;
});

async function getPeers(statusNode, multiaddrNode) {
// Display status
statusDiv.innerHTML = "<p>Discovering peers</p>";

// Clear all options in select element
const selectElement = document.getElementById("peer-select");
selectElement.innerHTML = "";

// Get peers using DNS discovery
const defaultNodeCount = 5;
const dnsDiscovery = await DnsNodeDiscovery.dnsOverHttp();
const peers = await dnsDiscovery.getPeers([enrTree["TEST"]], {
relay: defaultNodeCount,
store: defaultNodeCount,
filter: defaultNodeCount,
lightPush: defaultNodeCount,
});

// Create an option element for each peer's multiaddr and append to select element
const optionElements = peers.map((peer) => {
const optionElement = document.createElement("option");
optionElement.value = `${peer.multiaddrs[1]}/p2p/${peer.peerId}`;
optionElement.text = `${peer.multiaddrs[1]}/p2p/${peer.peerId}`;
return optionElement;
});
selectElement.append(...optionElements);

// Set first peer as selected
selectElement.options[0].selected = true;
multiaddrNode.value = selectElement.options[0].value;
}

async function searchForPeer(statusNode, multiaddrNode) {
statusDiv.innerHTML = "<p>Discovering peer</p>";

Expand Down

0 comments on commit 6a11881

Please sign in to comment.