Skip to content

Add new useRpc hook prototype#1314

Merged
1egoman merged 28 commits intomainfrom
use-rpc-hook-prototype
Apr 15, 2026
Merged

Add new useRpc hook prototype#1314
1egoman merged 28 commits intomainfrom
use-rpc-hook-prototype

Conversation

@1egoman
Copy link
Copy Markdown
Contributor

@1egoman 1egoman commented Mar 25, 2026

Overview

A prototype of a new useRpc hook aiming to simplify both sending and responding to rpc messages.

A snapshot build was published on npm, run the below to give it a shot locally:

$ npm install --save @livekit/components-react@rpc-prototype

Example usage:

Here's the server end:

// "Server" end of RPC assumes json encoded rpc requests / responses
useRpc(session, "getUserLocation", async (payload: { highAccuracy: boolean }, data) => {
  const position = await new Promise<GeolocationPosition>((resolve, reject) => {
    navigator.geolocation.getCurrentPosition(resolve, reject, {
      enableHighAccuracy: payload.highAccuracy,
      timeout: data.responseTimeout * 1000,
    });
  });
  return {
    latitude: position.coords.latitude,
    longitude: position.coords.longitude,
  };
});

// Alternative: custom request / response serialization:
useRpc(
  session,
  "getUserLocation",
  async (payload, data) => {
    return "raw text";
  },
  // To go back to raw serialization, pass serializers.raw() explicitly here:
  { serializer: serializers.raw() },
);

// Alternative: custom request / response serialization:
useRpc(
  session,
  "getUserLocation",
  async (payload /* <= decoded from base64 */, data) => {
    return "text to base64 encode";
  },
  {
    // Fully custom serializer example (base64):
    serializer: serializers.custom({
      parse: (raw) => window.atob(raw),
      serialize: (base64) => window.btoa(base64),
    })
  }
);

And the client end:

const { performRpc } = useRpc();

// The "Client" end of RPC also defaults to json:
const result = await performRpc({
  destinationIdentity: participantIdentity,
  method: 'getUserLocation',
  payload: { highAccuracy: true },
});

// Example of using a serializer in the client end as well:
const result = await performRpc({
  destinationIdentity: participantIdentity,
  method: 'getUserLocation',
  payload: "raw text",
}, serializers.raw());

Todo

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 25, 2026

size-limit report 📦

Path Size
LiveKitRoom only 6.01 KB (0%)
LiveKitRoom with VideoConference 32.24 KB (0%)
All exports 43.45 KB (+0.88% 🔺)

@vercel
Copy link
Copy Markdown

vercel bot commented Mar 25, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
components-js-storybook-5kld Ready Ready Preview, Comment Apr 15, 2026 4:26pm

Request Review

@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Mar 27, 2026

🦋 Changeset detected

Latest commit: f5acb22

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 6 packages
Name Type
@livekit/components-react Patch
@agents-ui Patch
@livekit/component-example-next Patch
@livekit/components-js-docs Patch
@livekit/component-docs-storybook Patch
@livekit/components-docs-gen Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@1egoman
Copy link
Copy Markdown
Contributor Author

1egoman commented Apr 9, 2026

Ok, I think this is finally in a good state to get some review eyes on. Lukas and I have talked about it fairly extensively though so I'm hoping it's fairly straightforward 🤞

Comment thread packages/react/src/hooks/useRpc.ts Outdated
Comment thread packages/react/src/hooks/useRpc.ts Outdated
Comment thread packages/react/src/hooks/useRpc.ts Outdated
Comment thread packages/react/src/hooks/useRpc.ts
Comment thread packages/react/src/hooks/useRpc.ts Outdated
1egoman added 20 commits April 15, 2026 10:52
…he value

This allows users to manually define a conforming object shape
themselves.
We came up with some fairly good improvements which I think make the
schema stuff less annoying and more homogeneous on both ends.
@1egoman 1egoman force-pushed the use-rpc-hook-prototype branch from b3548ab to 5f48e20 Compare April 15, 2026 16:21
@1egoman
Copy link
Copy Markdown
Contributor Author

1egoman commented Apr 15, 2026

(rebased on top of latest main)

@1egoman
Copy link
Copy Markdown
Contributor Author

1egoman commented Apr 15, 2026

@lukasIO I believe all the comments we had both left should be addressed - any concerns with merging this as is? If not, can you give me a ✅ ?

@1egoman 1egoman merged commit 91bb48c into main Apr 15, 2026
7 checks passed
@1egoman 1egoman deleted the use-rpc-hook-prototype branch April 15, 2026 16:55
@github-actions github-actions bot mentioned this pull request Apr 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants