"use client" import { Button } from '@/components/ui/button'; import { v4 } from "uuid" import { useEffect, useState } from 'react'; import useWebSocket from 'react-use-websocket'; const socketUrl = 'wss://echo.websocket.org'; const SocketPage = () => { const [mounted, setMounted] = useState(false) const { sendMessage, sendJsonMessage, lastMessage, lastJsonMessage, readyState, getWebSocket, } = useWebSocket(socketUrl, { onOpen: () => console.log('opened'), onMessage: (e) => console.log("MESSAGE", e), onError: (e) => console.error(e), shouldReconnect: (closeEvent) => { console.log("Reconnect"); return true; }, }); const [messageHistory, setMessageHistory] = useState([]); useEffect(() => { if (lastMessage !== null) { console.log("LAST", lastMessage) setMessageHistory((prev) => prev.concat(lastMessage)); } }, [lastMessage, setMessageHistory]); useEffect(() => { if (!mounted) { setMounted(true) } }, []) return (

Hello

{readyState}

{lastMessage ? Last message: {lastMessage.data} : null}
    {messageHistory.map((message, idx) => (

    {message ? message.data : null}

    ))}
) } export default SocketPage