UNPKG

@fishjam-cloud/react-client

Version:
40 lines (39 loc) 1.35 kB
import { useCallback, useContext } from "react"; import { FishjamClientContext } from "../contexts/fishjamClient"; import { PeerStatusContext } from "../contexts/peerStatus"; import { useReconnection } from "./internal/useReconnection"; /** * Hook allows to join or leave a room and check the current connection status. * @category Connection * @group Hooks */ export function useConnection() { const fishjamClientRef = useContext(FishjamClientContext); if (!fishjamClientRef) throw Error("useConnection must be used within FishjamProvider"); const peerStatus = useContext(PeerStatusContext); const client = fishjamClientRef.current; const reconnectionStatus = useReconnection(); const joinRoom = useCallback(({ url, peerToken, peerMetadata, }) => client.connect({ url, token: peerToken, peerMetadata: peerMetadata ?? {} }), [client]); const leaveRoom = useCallback(() => { client.disconnect(); }, [client]); return { /** * Join room and start streaming camera and microphone */ joinRoom, /** * Leave room and stop streaming */ leaveRoom, /** * Current peer connection status */ peerStatus, /** * Current reconnection status */ reconnectionStatus, }; }