wip: Demonstrate App.tsx connecting to local Web RTC Direct node

This commit is contained in:
Franck Royer 2022-02-28 17:28:44 +11:00
parent 20de4107bc
commit b8b4d552ac
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
5 changed files with 77 additions and 20 deletions

View File

@ -0,0 +1,35 @@
import Bootstrap from 'libp2p-bootstrap';
import PeerId from 'peer-id';
import { Waku } from 'js-waku';
import WebRTCDirect from 'libp2p-webrtc-direct';
;
(async () => {
const hardcodedPeerId = await PeerId.createFromJSON({
'id': '12D3KooWCuo3MdXfMgaqpLC5Houi1TRoFqgK9aoxok4NK5udMu8m',
'privKey': 'CAESQAG6Ld7ev6nnD0FKPs033/j0eQpjWilhxnzJ2CCTqT0+LfcWoI2Vr+zdc1vwk7XAVdyoCa2nwUR3RJebPWsF1/I=',
'pubKey': 'CAESIC33FqCNla/s3XNb8JO1wFXcqAmtp8FEd0SXmz1rBdfy'
});
const waku = await Waku.create({
libp2p: {
peerId: hardcodedPeerId,
addresses: {
listen: ['/ip4/127.0.0.1/tcp/9090/http/p2p-webrtc-direct']
},
modules: {
transport: [WebRTCDirect]
},
}
});
waku.libp2p.connectionManager.on('peer:connect', (connection) => {
console.info(`Connected to ${connection.remotePeer.toB58String()}!`);
});
console.log('Listening on:');
waku.libp2p.multiaddrs.forEach((ma) => console.log(`${ma.toString()}/p2p/${waku.libp2p.peerId.toB58String()}`));
})();

29
examples/waku-node/package-lock.json generated Normal file
View File

@ -0,0 +1,29 @@
{
"name": "@waku/node",
"version": "1.0.0",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "@waku/node",
"version": "1.0.0",
"license": "ISC",
"dependencies": {
"js-waku": "file:../../build/main"
}
},
"../../build/esm": {
"extraneous": true
},
"../../build/main": {},
"node_modules/js-waku": {
"resolved": "../../build/main",
"link": true
}
},
"dependencies": {
"js-waku": {
"version": "file:../../build/main"
}
}
}

View File

@ -3,9 +3,14 @@
"version": "1.0.0", "version": "1.0.0",
"description": "", "description": "",
"main": "index.js", "main": "index.js",
"private": true,
"scripts": { "scripts": {
"start": "node ./index.mjs",
"test": "echo \"Error: no test specified\" && exit 1" "test": "echo \"Error: no test specified\" && exit 1"
}, },
"author": "", "author": "",
"license": "ISC" "license": "MIT OR Apache-2.0",
"dependencies": {
"js-waku": "file:../../build/main"
}
} }

View File

@ -1,18 +1,13 @@
import { useEffect, useReducer, useState } from "react"; import { useEffect, useReducer, useState } from "react";
import "./App.css"; import "./App.css";
import { import { PageDirection, Waku, WakuMessage } from "js-waku";
PageDirection,
getPredefinedBootstrapNodes,
Waku,
WakuMessage,
} from "js-waku";
import handleCommand from "./command"; import handleCommand from "./command";
import Room from "./Room"; import Room from "./Room";
import { WakuContext } from "./WakuContext"; import { WakuContext } from "./WakuContext";
import { ThemeProvider } from "@livechat/ui-kit"; import { ThemeProvider } from "@livechat/ui-kit";
import { generate } from "server-name-generator"; import { generate } from "server-name-generator";
import { Message } from "./Message"; import { Message } from "./Message";
import { Fleet } from "js-waku/lib/discovery/predefined"; import { Protocols } from "js-waku/lib/waku";
const themes = { const themes = {
AuthorName: { AuthorName: {
@ -130,8 +125,7 @@ export default function App() {
if (historicalMessagesRetrieved) return; if (historicalMessagesRetrieved) return;
const retrieveMessages = async () => { const retrieveMessages = async () => {
await waku.waitForRemotePeer(); await waku.waitForRemotePeer([Protocols.Relay]);
console.log(`Retrieving archived messages`);
try { try {
retrieveStoreMessages(waku, dispatchMessages).then((length) => { retrieveStoreMessages(waku, dispatchMessages).then((length) => {
@ -185,7 +179,9 @@ async function initWaku(setter: (waku: Waku) => void) {
}, },
}, },
bootstrap: { bootstrap: {
peers: getPredefinedBootstrapNodes(selectFleetEnv()), peers: [
`/ip4/127.0.0.1/tcp/9090/http/p2p-webrtc-direct/p2p/12D3KooWCuo3MdXfMgaqpLC5Houi1TRoFqgK9aoxok4NK5udMu8m`,
],
}, },
}); });
@ -195,15 +191,6 @@ async function initWaku(setter: (waku: Waku) => void) {
} }
} }
function selectFleetEnv() {
// Works with react-scripts
if (process?.env?.NODE_ENV === "development") {
return Fleet.Test;
} else {
return Fleet.Prod;
}
}
function reduceMessages(state: Message[], newMessages: Message[]) { function reduceMessages(state: Message[], newMessages: Message[]) {
return state.concat(newMessages); return state.concat(newMessages);
} }

View File

@ -8,6 +8,7 @@
"exports": "./", "exports": "./",
"repository": "https://github.com/status-im/js-waku", "repository": "https://github.com/status-im/js-waku",
"license": "MIT OR Apache-2.0", "license": "MIT OR Apache-2.0",
"exports": "./",
"keywords": [ "keywords": [
"waku", "waku",
"decentralised", "decentralised",