Skip to content

Commit dbaf7a9

Browse files
authored
Initial draft of tokensource readme docs (#1681)
1 parent f7ebb2f commit dbaf7a9

File tree

1 file changed

+105
-1
lines changed

1 file changed

+105
-1
lines changed

README.md

Lines changed: 105 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,13 @@ const room = new Room({
9595
},
9696
});
9797

98+
// get your url from livekit's dashboard, or point it at a self hosted livekit deployment
99+
const url = "ws://localhost:7800";
100+
101+
// generate a token by making a request to a endpoint using the livekit server sdk or
102+
// using a prebuilt TokenSource (documented below)
103+
const token = "...";
104+
98105
// pre-warm connection, this can be called as early as your page is loaded
99106
room.prepareConnection(url, token);
100107

@@ -107,7 +114,7 @@ room
107114
.on(RoomEvent.LocalTrackUnpublished, handleLocalTrackUnpublished);
108115

109116
// connect to room
110-
await room.connect('ws://localhost:7800', token);
117+
await room.connect(url, token);
111118
console.log('connected to room', room.name);
112119

113120
// publish local camera and mic tracks
@@ -304,6 +311,103 @@ setLogExtension((level: LogLevel, msg: string, context: object) => {
304311
});
305312
```
306313

314+
### Generating a url/token with `TokenSource`
315+
316+
A TokenSource is a pre-implemented way of fetching credentials. Once a `TokenSource` is constructed, call
317+
`fetch` to generate a new set of credentials.
318+
319+
There are two types of `TokenSource`'s - fixed and configurable. Configurable token sources can be
320+
passed options as part of the generation process, allowing you to customize the token that they
321+
generate. Fixed token sources generate static credentials and don't accept parameters that can
322+
effect the generated token.
323+
324+
```ts
325+
// Fixed token sources don't take any parameters as part of `fetch`:
326+
const fixed: TokenSourceFixed = /* ... */;
327+
const fixedResponse = await fixed.fetch();
328+
room.connect(fixedResponse.serverUrl, fixedResponse.participantToken);
329+
330+
// Configurable token sources can optionally take parameters to change what is encoded into the token:
331+
const configurable: TokenSourceConfigurable = /* ... */;
332+
const configurableResponse = await configurable.fetch({ agentName: "agent to dispatch" } /* <-- here */);
333+
room.connect(configurableResponse.serverUrl, configurableResponse.participantToken);
334+
```
335+
336+
|Mechanism: | using pre-generated credentials | via a http request to a url | via fully custom logic |
337+
|-------------|--|--|--|
338+
|Fixed | [`TokenSource.literal`](#tokensourceliteral) | &mdash; | [`TokenSource.literal(async () => { /* ... */ })`](#tokensourceliteral) |
339+
|Configurable | &mdash; | [`TokenSource.endpoint`](#tokensourceendpoint) or [`TokenSource.sandboxTokenServer`](#tokensourceendpoint) | [`TokenSource.custom`](#tokensourcecustom) |
340+
341+
#### TokenSource.Literal
342+
A fixed token source which returns a static set of credentials or a computed set of credentials
343+
with no external input required on each call.
344+
345+
Example:
346+
```ts
347+
const literal1 = TokenSource.literal({ serverUrl: "ws://localhost:7800", participantToken: "..." });
348+
await literal1.fetch() // { serverUrl: "ws://localhost:7800", participantToken: "..." }
349+
350+
const literal2 = TokenSource.literal(async () => ({ serverUrl: "ws://localhost:7800", participantToken: "..." }));
351+
await literal2.fetch() // { serverUrl: "ws://localhost:7800", participantToken: "..." }
352+
```
353+
354+
#### TokenSource.Endpoint
355+
A configurable token source which makes a request to an endpoint to generate credentials. By
356+
default, a `POST` request with a `Content-Type: application/json` header is made, and the request
357+
body is expected to follow the [standard token format](https://cloud.livekit.io/projects/p_/sandbox/templates/token-server). If
358+
credentials generation is successful, the endpoint returns a 2xx status code with a body following
359+
the [standard token response format](https://cloud.livekit.io/projects/p_/sandbox/templates/token-server).
360+
361+
Example:
362+
```ts
363+
const endpoint1 = TokenSource.endpoint("http://example.com/credentials-endpoint");
364+
await endpoint1.fetch({ agentName: "agent to dispatch" }) // { serverUrl: "...", participantToken: "... token encoding agentName ..." }
365+
366+
const endpoint2 = TokenSource.endpoint("http://example.com/credentials-endpoint", {
367+
// For all supported options below, see https://developer.mozilla.org/en-US/docs/Web/API/RequestInit
368+
method: "PUT",
369+
headers: {
370+
"X-Custom-Header": "custom header value",
371+
},
372+
});
373+
await endpoint2.fetch({ agentName: "agent to dispatch" }) // { serverUrl: "...", participantToken: "... token encoding agentName ..." }
374+
```
375+
376+
#### TokenSource.SandboxTokenServer
377+
A configurable token source which makes a request to a
378+
[sandbox token server endpoint](https://cloud.livekit.io/projects/p_/sandbox/templates/token-server),
379+
a LiveKit-hosted token generation mechanism.
380+
381+
This token generation mechanism is inherently insecure and should only be used for
382+
prototyping; do NOT use in production.
383+
384+
One parameter is required - the sandbox id from the dashboard. This is the `token-server-xxxxxx`
385+
value in `https://token-server-xxxxxx.sandbox.livekit.io`.
386+
387+
Example:
388+
```ts
389+
const sandbox = TokenSource.sandboxTokenServer("token-server-xxxxxx");
390+
await sandbox.fetch({ agentName: "agent to dispatch" }); // { serverUrl: "...", participantToken: "... token encoding agentName ..." }
391+
```
392+
393+
#### TokenSource.Custom
394+
A fully custom configurable token source that allows you to consume any end application-specific
395+
token generation mechanism. Tokens that are generated are cached and used until they expire or the
396+
options passed into `fetch` change.
397+
398+
Note that it is expected that all options passed into `fetch` will always be encoded into the
399+
output token. If you'd rather implement a fixed version of this TokenSource, see
400+
`TokenSource.literal(async () => { /* ... */ })`.
401+
402+
Example:
403+
```ts
404+
const sandbox = TokenSource.custom(async (options) => {
405+
// generate token info via custom means here
406+
return { serverUrl: "...", participantToken: "... options encoded in here ..." };
407+
});
408+
await sandbox.fetch({ agentName: "agent to dispatch" });
409+
```
410+
307411
### RPC
308412

309413
Perform your own predefined method calls from one participant to another.

0 commit comments

Comments
 (0)