import React, { useEffect, useState } from 'react'; import logo from './logo.svg'; import './App.scss'; import { Fluence } from '@fluencelabs/js-client.api'; import type { ConnectionState } from '@fluencelabs/js-client.api'; import { kras } from '@fluencelabs/fluence-network-environment'; import { sayHello, registerHelloPeer } from './_aqua/getting-started'; const relayNodes = [kras[4], kras[5], kras[6]]; function App() { const [connectionState, setConnectionState] = useState('disconnected'); const [peerInfo, setPeerInfo] = useState<{ peerId: string; relayPeerId: string } | null>(null); const [helloMessage, setHelloMessage] = useState(null); const [peerIdInput, setPeerIdInput] = useState(''); const [relayPeerIdInput, setRelayPeerIdInput] = useState(''); useEffect(() => { Fluence.onConnectionStateChange((state) => { console.log('Connection state changed to: ', state); setConnectionState(state); if (state === 'connected') { Fluence.getClient().then((client) => { const peerId = client.getPeerId(); const relayPeerId = client.getRelayPeerId(); setPeerInfo({ peerId, relayPeerId }); }); } }); }, []); const connect = async (relayPeerId: string) => { try { await Fluence.connect(relayPeerId); // Register handler for this call in aqua: // HelloPeer.hello(%init_peer_id%) registerHelloPeer({ hello: (from) => { setHelloMessage('Hello from: \n' + from); return 'Hello back to you, \n' + from; }, }); } catch (err) { console.log('Client could not connect', err); } }; const helloBtnOnClick = async () => { if (connectionState !== 'connected') { return; } // Using aqua is as easy as calling a javascript function const res = await sayHello(peerIdInput, relayPeerIdInput); setHelloMessage(res); }; const isConnected = connectionState === 'connected'; return (
logo

{connectionState}

{isConnected ? ( <>
Peer id: {peerInfo?.peerId}
Relay peer id: {peerInfo?.relayPeerId}

Say hello!

Now try opening a new tab with the same application. Copy paste the peer id and relay from the second tab and say hello!

setPeerIdInput(e.target.value)} value={peerIdInput} />
setRelayPeerIdInput(e.target.value)} value={relayPeerIdInput} />
) : ( <>

Intro 1: P2P browser-to-browser

Pick a relay

    {relayNodes.map((x) => (
  • {x.peerId}
  • ))}
)} {helloMessage && ( <>

Message

{helloMessage}
)}
); } const copyToClipboard = (text?: string) => { if (text) { navigator.clipboard.writeText(text); } }; export default App;