useRpc
function useRpc<S>(
_session,
_methodName,
handler): UseRpcReturn<S, S>;Defined in: packages/react/src/hooks/room/useRpc.ts:42
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
Type Parameters
| Type Parameter | Default type |
|---|---|
S | unknown |
Parameters
| Parameter | Type |
|---|---|
_session | unknown |
_methodName | string |
handler | RpcHandler<S, S> |
Returns
UseRpcReturn<S, S>
Example
// Basic — register an RPC handler on a session.
const { performRpc } = useRpc(async (payload: { highAccuracy: boolean }) => getPosition(payload), "getUserLocation", session);
// Advanced — call an agent method from a room action.
function LocationButton({ session }: { session: UseSessionReturn }) {
const { performRpc } = useRpc(
async (payload: { highAccuracy: boolean }) => getPosition(payload),
"getUserLocation",
session,
);
return <button onClick={() => performRpc({ destinationIdentity: "myAgent", payload: { highAccuracy: true } })}>Share location</button>;
}