Skip to main content
GitHub

Source

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

handler
Required
RpcHandler<SerializerInput<S>, SerializerOutput<S>>
methodName
Required
string
session
Required
UseSessionReturn

Returns

UseRpcReturn;