build(relay-js): bump js-waku to next

This commit is contained in:
fryorcraken.eth 2022-08-31 13:05:59 +10:00
parent fcaf17d953
commit b48a65f87d
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4

View File

@ -2,9 +2,9 @@
<html lang='en'> <html lang='en'>
<head> <head>
<meta charset='UTF-8' /> <meta charset='UTF-8'/>
<meta content='width=device-width, initial-scale=1.0' name='viewport' /> <meta content='width=device-width, initial-scale=1.0' name='viewport'/>
<title>JS-Waku Chat</title> <title>JS-Waku Chat</title>
</head> </head>
<body> <body>
@ -13,114 +13,88 @@
<div id='status'></div> <div id='status'></div>
<input id='textInput' placeholder='Type your message here' type='text'> <input id='textInput' placeholder='Type your message here' type='text'>
<button id='sendButton' onclick='sendMessage();' type='button'>Send Message <button id='sendButton' type='button'>Send Message
</button> </button>
<div><h1>Messages</h1></div> <div><h1>Messages</h1></div>
<div id='messages'></div> <div id='messages'></div>
<script <script type='module'>
src='https://unpkg.com/js-waku@latest/build/umd/js-waku.min.bundle.js'></script> /**
<script> * 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.
* 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.
*
* - API documentation: https://js-waku.wakuconnect.dev/
* - Guides: https://docs.wakuconnect.dev/
*
* Note: open this HTML in two different browsers to experience decentralized communication.
* A node will not show its own messages, this can be changed by modifying the `Waku.create` call:
*
* Waku.create({
* bootstrap: {default: true}, libp2p: {
* config: {
* pubsub: {
* enabled: true,
* emitSelf: true,
* },
* },
* },
* })
*
*/
const { Waku, WakuMessage } = jswaku; import {
const statusDiv = document.getElementById('status'); waitForRemotePeer,
const messagesDiv = document.getElementById('messages'); WakuMessage
const textInput = document.getElementById('textInput'); } from 'https://unpkg.com/js-waku@next/bundle/index.js';
const sendButton = document.getElementById('sendButton'); import {
createWaku
} from 'https://unpkg.com/js-waku@next/bundle/lib/create_waku.js'
// Keep it disabled until Waku node is ready const statusDiv = document.getElementById('status');
textInput.disabled = true; const messagesDiv = document.getElementById('messages');
sendButton.disabled = true; const textInput = document.getElementById('textInput');
const sendButton = document.getElementById('sendButton');
// Every Waku Message has a content topic that categorizes it. // Keep it disabled until Waku node is ready
// It is always encoded in clear text. textInput.disabled = true;
// Recommendation: `/dapp-name/version/functionality/codec` sendButton.disabled = true;
// 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';
// Function to be used to send the text input over Waku. // Every Waku Message has a content topic that categorizes it.
let sendMessage = () => { // 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 { try {
statusDiv.innerHTML = '<p>Starting</p>'; statusDiv.innerHTML = '<p>Starting</p>';
// Create and starts a Waku node. // Create and starts a Waku node.
// `default: true` bootstraps by connecting to pre-defined/hardcoded Waku nodes. // `default: true` bootstraps by connecting to pre-defined/hardcoded Waku nodes.
// We are currently working on migrating this method to DNS Discovery. // We are currently working on migrating this method to DNS Discovery.
//
// https://js-waku.wakuconnect.dev/classes/waku.Waku.html#create
Waku.create({ bootstrap: { default: true } }).catch(e => {
statusDiv.innerHTML = 'Error';
console.log('Issue starting Waku node', e);
}
).then(wakuNode => {
// Had a hook to process all incoming messages on a specified content topic.
//
// https://js-waku.wakuconnect.dev/classes/waku_relay.WakuRelay.html#addObserver
wakuNode.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 // https://js-waku.wakuconnect.dev/classes/waku.Waku.html#create
if (!wakuMessage.payload) const waku = await createWaku({defaultBootstrap: true});
return; await waku.start();
// Helper method to decode the payload to utf-8. A production dApp should // Had a hook to process all incoming messages on a specified content topic.
// use `wakuMessage.payload` (Uint8Array) which enables encoding a data
// structure of their choice.
// //
// https://js-waku.wakuconnect.dev/classes/waku_message.WakuMessage.html#payloadAsUtf8 // https://js-waku.wakuconnect.dev/classes/waku_relay.WakuRelay.html#addObserver
const text = wakuMessage.payloadAsUtf8; waku.relay.addObserver((wakuMessage) => {
messagesDiv.innerHTML = `<p>${text}</p><br />` + messagesDiv.innerHTML;
}, [contentTopic]);
statusDiv.innerHTML = '<p>Connecting to a peer</p>'; // 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;
// Best effort method that waits for the Waku node to be connected to remote // Helper method to decode the payload to utf-8. A production dApp should
// waku nodes (peers) and for appropriate handshakes to be done. // use `wakuMessage.payload` (Uint8Array) which enables encoding a data
// // structure of their choice.
// https://js-waku.wakuconnect.dev/classes/waku.Waku.html#waitForRemotePeer //
wakuNode.waitForRemotePeer() // https://js-waku.wakuconnect.dev/classes/waku_message.WakuMessage.html#payloadAsUtf8
.catch((e) => { const text = wakuMessage.payloadAsUtf8;
statusDiv.innerHTML = 'Failed to connect to peers: ' + e.toString(); messagesDiv.innerHTML = `<p>${text}</p><br />` + messagesDiv.innerHTML;
}) }, [contentTopic]);
.then(() => {
// We are now connected to a remote peer, let's define the `sendMessage` statusDiv.innerHTML = '<p>Connecting to a peer</p>';
// function that sends the text input over Waku Relay, the gossipsub
// protocol. // Best effort method that waits for the Waku node to be connected to remote
sendMessage = () => { // 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 () => {
const text = textInput.value; const text = textInput.value;
// Reset the text input.
textInput.value = null;
// Helper functions are available to create a Waku Message. // Helper functions are available to create a Waku Message.
// These functions also provide native symmetric, asymmetric encryption, // These functions also provide native symmetric, asymmetric encryption,
@ -131,32 +105,26 @@
// serialize a data structure. // serialize a data structure.
// //
// https://js-waku.wakuconnect.dev/classes/waku_message.WakuMessage.html#fromUtf8String // https://js-waku.wakuconnect.dev/classes/waku_message.WakuMessage.html#fromUtf8String
WakuMessage.fromUtf8String(text, contentTopic).catch(e => console.log('Error encoding message', e)).then( const wakuMessage = await WakuMessage.fromUtf8String(text, contentTopic);
wakuMessage => { // Once the message is constructed, send it over Waku Relay.
// Once the message is constructed, send it over Waku Relay. //
// // https://js-waku.wakuconnect.dev/classes/waku_relay.WakuRelay.html#send
// https://js-waku.wakuconnect.dev/classes/waku_relay.WakuRelay.html#send await waku.relay.send(wakuMessage);
wakuNode.relay.send(wakuMessage).catch((e) => { console.log('Message sent!');
console.log('Error sending message', e);
}).then(() => {
console.log('Message sent', text);
});
}
);
}; // Reset the text input.
textInput.value = null;
};
// Ready to send & receive messages, enable text input. // Ready to send & receive messages, enable text input.
textInput.disabled = false; textInput.disabled = false;
sendButton.disabled = false; sendButton.disabled = false;
statusDiv.innerHTML = '<p>Ready!</p>'; statusDiv.innerHTML = '<p>Ready!</p>';
}); } catch (e) {
}); statusDiv.innerHTML = 'Failed to start application';
} catch (e) { console.log(e);
timestampDiv.innerHTML = 'Failed to start application'; }
console.log(e);
}
</script> </script>
</body> </body>