From 99513484f5c3bd25d307ac1f19380adc90b1274e Mon Sep 17 00:00:00 2001 From: danisharora099 Date: Mon, 7 Aug 2023 17:02:29 +0530 Subject: [PATCH] feat: allow good peers to be saved and dial on reloads --- examples/web-chat/src/Room.tsx | 19 +++++++++++++++++++ examples/web-chat/src/hooks.ts | 3 ++- examples/web-chat/src/utils.ts | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) diff --git a/examples/web-chat/src/Room.tsx b/examples/web-chat/src/Room.tsx index ba030f1..372cc90 100644 --- a/examples/web-chat/src/Room.tsx +++ b/examples/web-chat/src/Room.tsx @@ -5,6 +5,8 @@ import { useWaku, useContentPair, useLightPush } from "@waku/react"; import { ChatMessage } from "./chat_message"; import { useNodePeers, usePeers } from "./hooks"; import type { RoomProps } from "./types"; +import { useEffect } from "react"; +import { getMultiaddrsFromLocalStorage } from "./utils"; export default function Room(props: RoomProps) { const { node } = useWaku(); @@ -41,6 +43,23 @@ export default function Room(props: RoomProps) { } }; + useEffect(() => { + if (!node) return; + + const multiaddrs = getMultiaddrsFromLocalStorage(); + (async () => { + console.log( + `Dialing ${multiaddrs.length} multiaddrs found in local storage` + ); + + try { + await Promise.all(multiaddrs.map((multiaddr) => node.dial(multiaddr))); + } catch (error) { + console.error(error); + } + })(); + }, [node]); + const allConnectedLength = orZero(allConnected?.length); const lightPushPeersLength = orZero(lightPushPeers?.length); const filterPeersLength = orZero(filterPeers?.length); diff --git a/examples/web-chat/src/hooks.ts b/examples/web-chat/src/hooks.ts index 8510ae8..c47cc49 100644 --- a/examples/web-chat/src/hooks.ts +++ b/examples/web-chat/src/hooks.ts @@ -11,7 +11,7 @@ import type { UsePeersParams, UsePeersResults, } from "./types"; -import { handleCatch } from "./utils"; +import { handleCatch, saveMultiaddrsToLocalStorage } from "./utils"; export const usePersistentNick = (): [ string, @@ -86,6 +86,7 @@ export const useNodePeers = (node: undefined | LightNode) => { }; const handleConnectPeerExchange = (event: CustomEvent) => { + saveMultiaddrsToLocalStorage(node.libp2p.peerStore.get(event.detail)); setConnectedPeerExchangePeers( (peers) => new Set([...peers, event.detail]) ); diff --git a/examples/web-chat/src/utils.ts b/examples/web-chat/src/utils.ts index ed54a12..5ae2303 100644 --- a/examples/web-chat/src/utils.ts +++ b/examples/web-chat/src/utils.ts @@ -1,4 +1,5 @@ import type { Peer } from "@libp2p/interface-peer-store"; +import { Multiaddr, multiaddr } from "@multiformats/multiaddr"; export async function handleCatch( promise?: Promise @@ -13,3 +14,35 @@ export async function handleCatch( return undefined; } } + +export async function saveMultiaddrsToLocalStorage( + peerPromise?: Promise +) { + let addresses: Peer["addresses"] = []; + + try { + addresses = (await peerPromise)?.addresses ?? []; + } catch (error) {} + + for (const addr of addresses) { + if (!addr.multiaddr.toString().includes("wss")) continue; + + localStorage.setItem( + `peer-exchange-${addr.multiaddr.toString()}`, + addr.multiaddr.toString() + ); + } +} + +export function getMultiaddrsFromLocalStorage(): Multiaddr[] { + const multiaddrs: Multiaddr[] = []; + for (const key in localStorage) { + if (key.startsWith("peer-exchange-")) { + const addr = localStorage.getItem(key); + if (addr) { + multiaddrs.push(multiaddr(addr)); + } + } + } + return multiaddrs; +}