diff --git a/examples/eth-dm/src/App.tsx b/examples/eth-dm/src/App.tsx
index 1d0de0594a..a452dbbb56 100644
--- a/examples/eth-dm/src/App.tsx
+++ b/examples/eth-dm/src/App.tsx
@@ -10,7 +10,6 @@ import { Message } from './messaging/Messages';
import 'fontsource-roboto';
import { AppBar, IconButton, Toolbar, Typography } from '@material-ui/core';
import KeyPairHandling from './key_pair_handling/KeyPairHandling';
-import InitWaku from './InitWaku';
import {
createMuiTheme,
ThemeProvider,
@@ -20,6 +19,13 @@ import { teal, purple, green } from '@material-ui/core/colors';
import WifiIcon from '@material-ui/icons/Wifi';
import BroadcastPublicKey from './BroadcastPublicKey';
import Messaging from './messaging/Messaging';
+import {
+ DirectMessageContentTopic,
+ handleDirectMessage,
+ handlePublicKeyMessage,
+ initWaku,
+ PublicKeyContentTopic,
+} from './waku';
declare let window: any;
@@ -85,6 +91,62 @@ function App() {
}
}, [address, signer]);
+ // Waku initialization
+ useEffect(() => {
+ if (waku) return;
+ initWaku()
+ .then((_waku) => {
+ console.log('waku: ready');
+ setWaku(_waku);
+ })
+ .catch((e) => {
+ console.error('Failed to initiate Waku', e);
+ });
+ }, [waku]);
+
+ useEffect(() => {
+ if (!waku) return;
+ if (!address) return;
+
+ const observerPublicKeyMessage = handlePublicKeyMessage.bind(
+ {},
+ address,
+ setPublicKeys
+ );
+
+ waku.relay.addObserver(observerPublicKeyMessage, [PublicKeyContentTopic]);
+
+ return function cleanUp() {
+ if (!waku) return;
+ waku.relay.deleteObserver(observerPublicKeyMessage, [
+ PublicKeyContentTopic,
+ ]);
+ };
+ }, [waku, address]);
+
+ useEffect(() => {
+ if (!waku) return;
+ if (!ethDmKeyPair) return;
+ if (!address) return;
+
+ const observerDirectMessage = handleDirectMessage.bind(
+ {},
+ setMessages,
+ ethDmKeyPair.privateKey,
+ address
+ );
+
+ waku.relay.addObserver(observerDirectMessage, [DirectMessageContentTopic]);
+
+ return function cleanUp() {
+ if (!waku) return;
+ if (!observerDirectMessage) return;
+ waku.relay.deleteObserver(observerDirectMessage, [
+ DirectMessageContentTopic,
+ ]);
+ };
+ }, [waku, address, ethDmKeyPair]);
+
let peers = 0;
if (waku) {
peers = waku.libp2p.connectionManager.connections.size;
@@ -123,14 +185,6 @@ function App() {
-