|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang='en'> |
| 3 | + |
| 4 | +<head> |
| 5 | + <meta charset='UTF-8' /> |
| 6 | + <meta content='width=device-width, initial-scale=1.0' name='viewport' /> |
| 7 | + <title>JS-Waku unpkg example</title> |
| 8 | +</head> |
| 9 | + |
| 10 | +<body> |
| 11 | + |
| 12 | +<div><h1>Timestamp of latest relay ping</h1></div> |
| 13 | +<div id='timestamp'></div> |
| 14 | + |
| 15 | +<script |
| 16 | + src=' https://unpkg.com/[email protected]/build/umd/js-waku.min.bundle.js' ></script> |
| 17 | +<script> |
| 18 | + /** |
| 19 | + * This example demonstrates how to use the js-waku minified bundle |
| 20 | + * available on unpkg.com. |
| 21 | + * |
| 22 | + * It is a simple scripts that uses Waku Store to retrieve ping relay messages |
| 23 | + * and displays the timestamp of the most recent ping relay message. |
| 24 | + * |
| 25 | + * More explanations are provided in this guide (TODO). |
| 26 | + */ |
| 27 | + const timestampDiv = document.getElementById('timestamp'); |
| 28 | + |
| 29 | + try { |
| 30 | + timestampDiv.innerHTML = '<p>Starting waku.</p>'; |
| 31 | + jswaku.Waku.create({ bootstrap: true }).catch(e => { |
| 32 | + timestampDiv.innerHTML = 'Failed to create Waku: ' + e.toString(); |
| 33 | + } |
| 34 | + ).then(waku => { |
| 35 | + timestampDiv.innerHTML = '<p>Connecting to a peer.</p>'; |
| 36 | + waku.waitForConnectedPeer() |
| 37 | + .catch((e) => { |
| 38 | + timestampDiv.innerHTML = 'Failed to connect to peers' + e.toString(); |
| 39 | + }) |
| 40 | + .then(() => { |
| 41 | + timestampDiv.innerHTML = '<p>Retrieving messages.</p>'; |
| 42 | + |
| 43 | + const callback = (wakuMessages) => { |
| 44 | + // Messages are ordered with oldest first |
| 45 | + // even with page direction `backward` |
| 46 | + const latestFirst = wakuMessages.reverse(); |
| 47 | + const latestMessage = latestFirst[0]; |
| 48 | + if (latestMessage) { |
| 49 | + timestampDiv.innerHTML = latestMessage.timestamp; |
| 50 | + } else { |
| 51 | + timestampDiv.innerHTML = '<p>No ping message available</p>'; |
| 52 | + } |
| 53 | + |
| 54 | + // When returning true, `queryHistory` stops retrieving pages |
| 55 | + // In our case, we only want one message, hence one page. |
| 56 | + return true; |
| 57 | + }; |
| 58 | + |
| 59 | + const startTime = new Date(); |
| 60 | + // Only retrieve a week of messages |
| 61 | + startTime.setTime(Date.now() - 7 * 24 * 60 * 60 * 1000); |
| 62 | + |
| 63 | + waku.store |
| 64 | + .queryHistory(['/relay-ping/1/ping/null'], { |
| 65 | + callback, |
| 66 | + pageDirection: 'backward', |
| 67 | + pageSize: 1, |
| 68 | + timeFilter: { |
| 69 | + startTime, |
| 70 | + endTime: new Date() |
| 71 | + } |
| 72 | + }) |
| 73 | + .catch((e) => { |
| 74 | + timestampDiv.innerHTML = 'Failed to retrieve messages' + e.toString(); |
| 75 | + }); |
| 76 | + }); |
| 77 | + }); |
| 78 | + } catch (e) { |
| 79 | + timestampDiv.innerHTML = 'Failed to start application' + e.toString(); |
| 80 | + } |
| 81 | +</script> |
| 82 | +</body> |
| 83 | + |
| 84 | +</html> |
0 commit comments