2022-06-17 00:48:15 +00:00
|
|
|
<!DOCTYPE html>
|
|
|
|
<html lang='en'>
|
|
|
|
|
|
|
|
<head>
|
2022-08-31 03:05:59 +00:00
|
|
|
<meta charset='UTF-8'/>
|
|
|
|
<meta content='width=device-width, initial-scale=1.0' name='viewport'/>
|
|
|
|
<title>JS-Waku Chat</title>
|
2022-06-17 00:48:15 +00:00
|
|
|
</head>
|
|
|
|
|
|
|
|
<body>
|
|
|
|
|
|
|
|
<div><h1>Waku Node Status</h1></div>
|
|
|
|
<div id='status'></div>
|
|
|
|
|
|
|
|
<input id='textInput' placeholder='Type your message here' type='text'>
|
2022-08-31 03:05:59 +00:00
|
|
|
<button id='sendButton' type='button'>Send Message
|
2022-06-17 00:48:15 +00:00
|
|
|
</button>
|
|
|
|
|
|
|
|
<div><h1>Messages</h1></div>
|
|
|
|
<div id='messages'></div>
|
|
|
|
|
|
|
|
|
2022-08-31 03:05:59 +00:00
|
|
|
<script type='module'>
|
|
|
|
/**
|
|
|
|
* Demonstrate usage of js-waku in the browser. Use relay, gossip sub protocol to send and receive messages.
|
|
|
|
* Recommended payload is protobuf. Using simple utf-8 string for demo purposes only.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import {
|
|
|
|
WakuMessage
|
2022-09-14 12:37:17 +00:00
|
|
|
} from 'https://unpkg.com/js-waku@0.27.0/bundle/index.js';
|
2022-08-31 03:05:59 +00:00
|
|
|
import {
|
2022-09-05 05:30:26 +00:00
|
|
|
createWaku
|
2022-09-14 12:37:17 +00:00
|
|
|
} from 'https://unpkg.com/js-waku@0.27.0/bundle/lib/create_waku.js'
|
2022-09-05 05:30:26 +00:00
|
|
|
import {
|
|
|
|
waitForRemotePeer
|
2022-09-14 12:37:17 +00:00
|
|
|
} from 'https://unpkg.com/js-waku@0.27.0/bundle/lib/wait_for_remote_peer.js'
|
2022-08-31 03:05:59 +00:00
|
|
|
|
|
|
|
const statusDiv = document.getElementById('status');
|
|
|
|
const messagesDiv = document.getElementById('messages');
|
|
|
|
const textInput = document.getElementById('textInput');
|
|
|
|
const sendButton = document.getElementById('sendButton');
|
|
|
|
|
|
|
|
// Keep it disabled until Waku node is ready
|
|
|
|
textInput.disabled = true;
|
|
|
|
sendButton.disabled = true;
|
|
|
|
|
|
|
|
// Every Waku Message has a content topic that categorizes it.
|
|
|
|
// It is always encoded in clear text.
|
|
|
|
// Recommendation: `/dapp-name/version/functionality/codec`
|
|
|
|
// We recommend to use protobuf as codec (`proto`), this demo uses utf-8
|
|
|
|
// for simplicity's sake.
|
|
|
|
const contentTopic = '/relay-demo/1/message/utf-8';
|
|
|
|
|
|
|
|
try {
|
|
|
|
statusDiv.innerHTML = '<p>Starting</p>';
|
|
|
|
|
|
|
|
// Create and starts a Waku node.
|
|
|
|
// `default: true` bootstraps by connecting to pre-defined/hardcoded Waku nodes.
|
|
|
|
// We are currently working on migrating this method to DNS Discovery.
|
2022-06-17 00:48:15 +00:00
|
|
|
//
|
2022-08-31 03:05:59 +00:00
|
|
|
// https://js-waku.wakuconnect.dev/classes/waku.Waku.html#create
|
|
|
|
const waku = await createWaku({defaultBootstrap: true});
|
|
|
|
await waku.start();
|
2022-06-17 00:48:15 +00:00
|
|
|
|
2022-08-31 03:05:59 +00:00
|
|
|
// Had a hook to process all incoming messages on a specified content topic.
|
2022-06-17 00:48:15 +00:00
|
|
|
//
|
2022-08-31 03:05:59 +00:00
|
|
|
// https://js-waku.wakuconnect.dev/classes/waku_relay.WakuRelay.html#addObserver
|
|
|
|
waku.relay.addObserver((wakuMessage) => {
|
|
|
|
|
|
|
|
// Checks there is a payload on the message.
|
|
|
|
// Waku Message is encoded in protobuf, in proto v3 fields are always optional.
|
|
|
|
//
|
|
|
|
// https://js-waku.wakuconnect.dev/classes/waku_message.WakuMessage.html#payload
|
|
|
|
if (!wakuMessage.payload)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Helper method to decode the payload to utf-8. A production dApp should
|
|
|
|
// use `wakuMessage.payload` (Uint8Array) which enables encoding a data
|
|
|
|
// structure of their choice.
|
|
|
|
//
|
|
|
|
// https://js-waku.wakuconnect.dev/classes/waku_message.WakuMessage.html#payloadAsUtf8
|
|
|
|
const text = wakuMessage.payloadAsUtf8;
|
|
|
|
messagesDiv.innerHTML = `<p>${text}</p><br />` + messagesDiv.innerHTML;
|
|
|
|
}, [contentTopic]);
|
|
|
|
|
|
|
|
statusDiv.innerHTML = '<p>Connecting to a peer</p>';
|
|
|
|
|
|
|
|
// Best effort method that waits for the Waku node to be connected to remote
|
|
|
|
// waku nodes (peers) and for appropriate handshakes to be done.
|
|
|
|
//
|
|
|
|
// https://js-waku.wakuconnect.dev/classes/waku.Waku.html#waitForRemotePeer
|
|
|
|
await waitForRemotePeer(waku);
|
|
|
|
|
|
|
|
// We are now connected to a remote peer, let's define the `sendMessage`
|
|
|
|
// function that sends the text input over Waku Relay, the gossipsub
|
|
|
|
// protocol.
|
|
|
|
sendButton.onclick = async () => {
|
2022-06-17 00:48:15 +00:00
|
|
|
const text = textInput.value;
|
|
|
|
|
|
|
|
// Helper functions are available to create a Waku Message.
|
|
|
|
// These functions also provide native symmetric, asymmetric encryption,
|
|
|
|
// signing and signature verification. Check the `Options` object for details:
|
|
|
|
// https://js-waku.wakuconnect.dev/interfaces/waku_message.Options.html
|
|
|
|
//
|
|
|
|
// `WakuMessage.fromBytes` should be preferred for a production dApp to
|
|
|
|
// serialize a data structure.
|
|
|
|
//
|
|
|
|
// https://js-waku.wakuconnect.dev/classes/waku_message.WakuMessage.html#fromUtf8String
|
2022-08-31 03:05:59 +00:00
|
|
|
const wakuMessage = await WakuMessage.fromUtf8String(text, contentTopic);
|
|
|
|
// Once the message is constructed, send it over Waku Relay.
|
|
|
|
//
|
|
|
|
// https://js-waku.wakuconnect.dev/classes/waku_relay.WakuRelay.html#send
|
|
|
|
await waku.relay.send(wakuMessage);
|
|
|
|
console.log('Message sent!');
|
|
|
|
|
|
|
|
// Reset the text input.
|
|
|
|
textInput.value = null;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Ready to send & receive messages, enable text input.
|
|
|
|
textInput.disabled = false;
|
|
|
|
sendButton.disabled = false;
|
|
|
|
statusDiv.innerHTML = '<p>Ready!</p>';
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
statusDiv.innerHTML = 'Failed to start application';
|
|
|
|
console.log(e);
|
|
|
|
}
|
2022-06-17 00:48:15 +00:00
|
|
|
</script>
|
|
|
|
</body>
|
|
|
|
|
|
|
|
</html>
|