+ `;
+ },
+ resetMessages: () => {
+ messagesBlock.innerHTML = "";
+ },
+ setWebrtcPeer: (peerId) => {
+ webrtcPeer.value = peerId;
+ },
+ onRemoteNodeConnect: (cb) => {
+ connectRemoteNode.addEventListener("click", () => {
+ const multiaddr = remoteNode.value;
+
+ if (!multiaddr) {
+ throw Error("No multiaddr set to dial");
+ }
+
+ cb(multiaddr);
+ });
+ },
+ onWebrtcConnect: (cb) => {
+ connectWebrtcPeer.addEventListener("click", () => {
+ const multiaddr = webrtcPeer.value;
+
+ if (!multiaddr) {
+ throw Error("No multiaddr to dial webrtc");
+ }
+
+ cb(multiaddr);
+ });
+ },
+ onRelayWebRTC: (cb) => {
+ relayWebRTCbutton.addEventListener("click", cb);
+ },
+ onDropNonWebRTC: (cb) => {
+ dropNonWebRTCbutton.addEventListener("click", cb);
+ },
+ setRelayMeshInfo: (gossipSub) => {
+ relayMeshInfo.innerHTML = "";
+
+ Array.from(gossipSub.peers)
+ .map((peerId) => {
+ let inbound = gossipSub.streamsInbound.get(peerId);
+ inbound = inbound ? inbound.rawStream.constructor.name : "none";
+
+ let outbound = gossipSub.streamsOutbound.get(peerId);
+ outbound = outbound ? outbound.rawStream.constructor.name : "none";
+
+ return [peerId, inbound, outbound];
+ })
+ .map(([peerId, inbound, outbound]) => {
+ relayMeshInfo.innerHTML += `${peerId}
inbound : ${inbound}\t
outbound : ${outbound}`;
+ relayMeshInfo.innerHTML += "
";
+ });
+ },
+ };
+}
diff --git a/examples/relay-rtc/manifest.json b/examples/relay-chat/manifest.json
similarity index 100%
rename from examples/relay-rtc/manifest.json
rename to examples/relay-chat/manifest.json
diff --git a/examples/relay-rtc/package-lock.json b/examples/relay-chat/package-lock.json
similarity index 100%
rename from examples/relay-rtc/package-lock.json
rename to examples/relay-chat/package-lock.json
diff --git a/examples/relay-rtc/package.json b/examples/relay-chat/package.json
similarity index 100%
rename from examples/relay-rtc/package.json
rename to examples/relay-chat/package.json
diff --git a/examples/relay-chat/style.css b/examples/relay-chat/style.css
new file mode 100644
index 0000000..77212d7
--- /dev/null
+++ b/examples/relay-chat/style.css
@@ -0,0 +1,185 @@
+* {
+ margin: 0;
+ padding: 0;
+ word-wrap: break-word;
+ box-sizing: border-box;
+}
+
+html,
+body {
+ width: 100%;
+ height: 100%;
+ max-width: 100%;
+ max-height: 100%;
+}
+
+html {
+ font-size: 16px;
+ overflow: hidden;
+}
+
+body {
+ display: flex;
+ align-items: center;
+ padding: 10px;
+ justify-content: center;
+}
+
+h3 + h4,
+div + h4,
+div + details,
+div + div {
+ margin-top: 20px;
+}
+
+.header div {
+ display: flex;
+ justify-content: space-between;
+}
+
+.header div input {
+ min-width: 300px;
+ min-height: 30px;
+ width: 80%;
+ margin-right: 10px;
+}
+
+.header div button {
+ min-width: 50px;
+ min-height: 30px;
+ width: 10%;
+ flex-grow: 1;
+}
+
+.header div button + button {
+ margin-left: 10px;
+}
+
+details {
+ margin-bottom: 15px;
+}
+
+details p {
+ margin-bottom: 10px;
+}
+
+summary {
+ cursor: pointer;
+ max-width: 100%;
+ margin-bottom: 5px;
+}
+
+span {
+ font-weight: 300;
+}
+
+input,
+textarea {
+ line-height: 1rem;
+ padding: 5px;
+}
+
+textarea {
+ min-height: 3rem;
+}
+
+h3 {
+ margin-bottom: 5px;
+}
+
+.content {
+ width: 800px;
+ min-width: 300px;
+ max-width: 800px;
+ height: 100%;
+ display: flex;
+ flex-direction: column;
+ align-content: space-between;
+}
+
+#messages {
+ overflow-y: scroll;
+ overflow-x: hidden;
+}
+
+.message + .message {
+ margin-top: 15px;
+}
+
+.message :first-child {
+ font-weight: bold;
+}
+
+.message p + p {
+ margin-top: 5px;
+}
+
+.message span {
+ font-size: 0.8rem;
+}
+
+.inputArea {
+ display: flex;
+ gap: 10px;
+ flex-direction: column;
+ margin-top: 20px;
+}
+
+.controls {
+ margin-top: 10px;
+ display: flex;
+ gap: 10px;
+}
+
+.controls button {
+ flex-grow: 1;
+ cursor: pointer;
+ padding: 10px;
+}
+
+#send {
+ background-color: #32d1a0;
+ border: none;
+ color: white;
+}
+#send:hover {
+ background-color: #3abd96;
+}
+#send:active {
+ background-color: #3ba183;
+}
+
+#exit {
+ color: white;
+ border: none;
+ background-color: #ff3a31;
+}
+#exit:hover {
+ background-color: #e4423a;
+}
+#exit:active {
+ background-color: #c84740;
+}
+
+.success {
+ color: #3ba183;
+}
+
+.progress {
+ color: #9ea13b;
+}
+
+.terminated {
+ color: black;
+}
+
+.error {
+ color: #c84740;
+}
+
+.footer {
+ display: flex;
+ width: 100%;
+ flex-direction: column;
+ align-self: flex-end;
+}
diff --git a/examples/relay-rtc/webpack.config.js b/examples/relay-chat/webpack.config.js
similarity index 71%
rename from examples/relay-rtc/webpack.config.js
rename to examples/relay-chat/webpack.config.js
index aa2eb4e..9fb628f 100644
--- a/examples/relay-rtc/webpack.config.js
+++ b/examples/relay-chat/webpack.config.js
@@ -13,7 +13,13 @@ module.exports = {
mode: "development",
plugins: [
new CopyWebpackPlugin({
- patterns: ["index.html", "favicon.ico", "favicon.png", "manifest.json"],
+ patterns: [
+ "index.html",
+ "favicon.ico",
+ "favicon.png",
+ "manifest.json",
+ "style.css",
+ ],
}),
],
};
diff --git a/examples/relay-rtc/index.html b/examples/relay-rtc/index.html
deleted file mode 100644
index 2d099c3..0000000
--- a/examples/relay-rtc/index.html
+++ /dev/null
@@ -1,42 +0,0 @@
-
-
-
-
-
-
JS-Waku light node example
-
-
-
-
-
-
-
Status
-
-
-
Local Peer Id
-
-
-
Remote Peer Id
-
-
-
Remote peer's multiaddr
-
-
Dial
-
-
Subscribe with Filter
-
- Unsubscribe with Filter
-
-
-
Message text
-
-
- Send message using Light Push
-
-
-
-
-
-
-
-
diff --git a/examples/relay-rtc/index.js b/examples/relay-rtc/index.js
deleted file mode 100644
index a523b56..0000000
--- a/examples/relay-rtc/index.js
+++ /dev/null
@@ -1,132 +0,0 @@
-import {
- createRelayNode,
- waitForRemotePeer,
- createEncoder,
- createDecoder,
- utf8ToBytes,
- bytesToUtf8,
-} from "@waku/sdk";
-import { webSockets } from "@libp2p/websockets";
-import { all as filterAll } from "@libp2p/websockets/filters";
-
-import { webRTC } from "@libp2p/webrtc";
-import { circuitRelayTransport } from "libp2p/circuit-relay";
-
-const peerIdDiv = document.getElementById("peer-id");
-const remotePeerIdDiv = document.getElementById("remote-peer-id");
-const statusDiv = document.getElementById("status");
-const remoteMultiAddrDiv = document.getElementById("remote-multiaddr");
-const dialButton = document.getElementById("dial");
-const subscribeButton = document.getElementById("subscribe");
-const unsubscribeButton = document.getElementById("unsubscribe");
-const messagesDiv = document.getElementById("messages");
-const textInput = document.getElementById("textInput");
-const sendButton = document.getElementById("sendButton");
-
-const ContentTopic = "/js-waku-examples/1/chat/utf8";
-const decoder = createDecoder(ContentTopic);
-const encoder = createEncoder({ contentTopic: ContentTopic });
-let messages = [];
-let unsubscribe;
-
-const updateMessages = (msgs, div) => {
- div.innerHTML = "
";
- messages.forEach((msg) => (div.innerHTML += "" + msg + " "));
- div.innerHTML += " ";
-};
-
-statusDiv.innerHTML = "
Creating Waku node.
";
-const node = await createRelayNode({
- libp2p: {
- addresses: {
- listen: ["/webrtc"],
- },
- connectionGater: {
- denyDialMultiaddr: () => {
- // by default we refuse to dial local addresses from the browser since they
- // are usually sent by remote peers broadcasting undialable multiaddrs but
- // here we are explicitly connecting to a local node so do not deny dialing
- // any discovered address
- return false;
- },
- },
- transports: [
- webRTC({}),
- circuitRelayTransport({
- discoverRelays: 1,
- }),
- webSockets({ filter: filterAll }),
- ],
- },
-});
-window.node = node;
-
-statusDiv.innerHTML = "
Starting Waku node.
";
-await node.start();
-statusDiv.innerHTML = "
Waku node started.
";
-peerIdDiv.innerHTML = "
" + node.libp2p.peerId.toString() + "
";
-dialButton.disabled = false;
-
-dialButton.onclick = async () => {
- const ma = remoteMultiAddrDiv.value;
- if (!ma) {
- statusDiv.innerHTML = "
Error: No multiaddr provided.
";
- return;
- }
- statusDiv.innerHTML = "
Dialing peer.
";
- const multiaddr = MultiformatsMultiaddr.multiaddr(ma);
- await node.dial(multiaddr, ["relay"]);
- await waitForRemotePeer(node, ["relay"]);
- const peers = await node.libp2p.peerStore.all();
- statusDiv.innerHTML = "
Peer dialed.
";
- remotePeerIdDiv.innerHTML = "
" + peers[0].id.toString() + "
";
- textInput.disabled = false;
- sendButton.disabled = false;
- subscribeButton.disabled = false;
-};
-
-const callback = (wakuMessage) => {
- const text = bytesToUtf8(wakuMessage.payload);
- const timestamp = wakuMessage.timestamp.toString();
- messages.push(text + " - " + timestamp);
- updateMessages(messages, messagesDiv);
-};
-
-subscribeButton.onclick = async () => {
- unsubscribe = await node.relay.subscribe([decoder], callback);
- unsubscribeButton.disabled = false;
- subscribeButton.disabled = true;
-};
-
-unsubscribeButton.onclick = async () => {
- await unsubscribe();
- unsubscribe = undefined;
- unsubscribeButton.disabled = true;
- subscribeButton.disabled = false;
-};
-
-sendButton.onclick = async () => {
- const text = textInput.value;
-
- await node.relay.send(encoder, {
- payload: utf8ToBytes(text),
- });
- console.log("Message sent!");
- textInput.value = null;
-};
-
-const GONODE = "16Uiu2HAmUdyH4P2UhgX3hTCbeeJHpxxfsn8EdyHkMnPyb54a92jZ";
-const root = `/ip4/192.168.0.101/tcp/60001/ws/p2p/${GONODE}`;
-
-remoteMultiAddrDiv.value = root;
-
-window.dial = (id) => node.dial(`${root}/p2p-circuit/webrtc/p2p/${id}`);
-
-window.drop = () =>
- Array.from(node.libp2p.components.connectionManager.connections.entries())
- .filter((c) => c[0].toString() === GONODE)
- .map((c) => console.log(c[1][0].close()));
-/*
- /ip4/127.0.0.1/tcp/60001/ws/p2p/16Uiu2HAm3s9fFHbcVrKQz2h5fMAsUzm3AeCUxg52e3SBFjt7q4Gg
- /ip4/127.0.0.1/tcp/60001/ws/p2p/16Uiu2HAm3s9fFHbcVrKQz2h5fMAsUzm3AeCUxg52e3SBFjt7q4Gg/p2p-circuit/p2p/12D3KooWMghpY8592CnQQTyg4fiSJcUuA7Pu8n8YzUPYaLjzH7Yo/p2p-circuit/webrtc/p2p/12D3KooWMghpY8592CnQQTyg4fiSJcUuA7Pu8n8YzUPYaLjzH7Yo
- */