import * as React from "react"; import protobuf from "protobufjs"; import { createRelayNode } from "@waku/create"; import { createDecoder, createEncoder, waitForRemotePeer } from "@waku/core"; const ContentTopic = `/js-waku-examples/1/chat/proto`; const Encoder = createEncoder(ContentTopic); const Decoder = createDecoder(ContentTopic); const SimpleChatMessage = new protobuf.Type("SimpleChatMessage") .add(new protobuf.Field("timestamp", 1, "uint32")) .add(new protobuf.Field("text", 2, "string")); function App() { const [waku, setWaku] = React.useState(undefined); const [wakuStatus, setWakuStatus] = React.useState("None"); // Using a counter just for the messages to be different const [sendCounter, setSendCounter] = React.useState(0); const [messages, setMessages] = React.useState([]); React.useEffect(() => { if (!!waku) return; if (wakuStatus !== "None") return; setWakuStatus("Starting"); (async () => { const waku = await createRelayNode({ defaultBootstrap: true }); setWaku(waku); await waku.start(); setWakuStatus("Connecting"); await waitForRemotePeer(waku, ["relay"]); setWakuStatus("Ready"); })(); }, [waku, wakuStatus]); const processIncomingMessage = React.useCallback((wakuMessage) => { console.log("Message received", wakuMessage); if (!wakuMessage.payload) return; const { text, timestamp } = SimpleChatMessage.decode(wakuMessage.payload); const time = new Date(); time.setTime(timestamp); const message = { text, timestamp: time }; setMessages((messages) => { return [message].concat(messages); }); }, []); React.useEffect(() => { if (!waku) return; // Pass the content topic to only process messages related to your dApp const deleteObserver = waku.relay.addObserver( Decoder, processIncomingMessage ); // Called when the component is unmounted, see ReactJS doc. return deleteObserver; }, [waku, wakuStatus, processIncomingMessage]); const sendMessageOnClick = () => { // Check Waku is started and connected first. if (wakuStatus !== "Ready") return; sendMessage(`Here is message #${sendCounter}`, waku, new Date()).then(() => console.log("Message sent") ); // For demonstration purposes. setSendCounter(sendCounter + 1); }; return (
{wakuStatus}
{msg.timestamp.toString()}: {msg.text}