useRpc
Note
This feature is under active development and may change based on developer feedback and real-world usage.
Hook for declarative RPC method registration and outbound RPC calls.
Registers a handler for an incoming RPC method and returns a performRpc function for outbound calls. The handler is registered on mount and unregistered on unmount. Handler identity does not matter (captured by ref), so inline functions work without useCallback.
Import
import { useRpc } from "@livekit/components-react";
Usage
const { performRpc } = useRpc(session,"getUserLocation",async (payload: { highAccuracy: boolean }) => {const pos = await getPosition(payload.highAccuracy);return { lat: pos.coords.latitude, lng: pos.coords.longitude };});
Properties
handlerRequired
RpcHandler<SerializerInput<S>, SerializerOutput<S>>methodNameRequired
stringsessionRequired
UseSessionReturnReturns
UseRpcReturn;