Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Copy this file to assets/.env and set your token server sandbox ID,
# found in your LiveKit Cloud project's Settings page.
# Copy this file to assets/.env and set your development token server ID,
# found on your LiveKit Cloud project's Settings page under "Development token server".
# See https://docs.livekit.io/frontends/build/authentication/development-token-server/
# Without it, the app connects to a default agent.
LIVEKIT_SANDBOX_ID=<your-sandbox-id>
LIVEKIT_TOKEN_SERVER_ID=<your-token-server-id>
19 changes: 12 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,26 @@ Note: You may need to configure signing certificates in Xcode if building to a r
The app is configured to connect to the LiveKit homepage agent by default, which you can also try at [livekit.com](https://www.livekit.com). To point the app at your own agent (see [Connect to your agent](#connect-to-your-agent)).

> [!NOTE]
> To setup without the LiveKit CLI, clone the repository and then either create the `assets/.env` file manually from a copy of `.env.example`. The env file is optional: without any configuration, the app connects to a default agent — the same one featured on the [LiveKit homepage](https://livekit.io) — so you can try it out right away.
> To setup without the LiveKit CLI, clone the repository and then create the `assets/.env` file manually from a copy of `.env.example`. The env file is optional: without any configuration, the app connects to a default agent — the same one featured on the [LiveKit homepage](https://livekit.io) — so you can try it out right away.

## Connect to your agent

To switch from the default agent to your own, you first need a LiveKit agent to speak with. For a no-code setup, use the [Agent Builder](https://docs.livekit.io/agents/start/builder/). For more customization, try our starter agent for [Python](https://github.com/livekit-examples/agent-starter-python), [Node.js](https://github.com/livekit-examples/agent-starter-node), or [create your own from scratch](https://docs.livekit.io/agents/start/voice-ai/).

Second, you need a token server. For development, the easiest option is the [sandbox token server](https://docs.livekit.io/frontends/authentication/tokens/sandbox-token-server/): enable it from your project's **Options** on the [Settings](https://cloud.livekit.io/projects/p_/settings/project) page in LiveKit Cloud and copy the `sandboxId`.
Second, you need a token server. For development, the easiest option is the [development token server](https://docs.livekit.io/frontends/build/authentication/development-token-server/): turn on the **Development token server** switch on the [Settings](https://cloud.livekit.io/projects/p_/settings/project) page in LiveKit Cloud and copy the **Token server ID**.

Then fill the `LIVEKIT_SANDBOX_ID` in your `assets/.env`:
Then fill the `LIVEKIT_TOKEN_SERVER_ID` in your `assets/.env`:

```swift
LIVEKIT_SANDBOX_ID=<your-sandbox-id>
```sh
LIVEKIT_TOKEN_SERVER_ID=<your-token-server-id>
```

or modify `lib/controllers/app_ctrl.dart` to replace the `SandboxTokenSource` with your own token source implementation (development-only hardcoded credentials are also supported there).
or modify `lib/controllers/app_ctrl.dart` to replace the `DevelopmentTokenSource` with your own token source implementation (development-only hardcoded credentials are also supported there).

> [!NOTE]
> The development token server is for prototyping only — any client can request a token with any permissions. See [Token generation in production](#token-generation-in-production) before you ship.
>
> This setting was previously called the *sandbox token server*, and `LIVEKIT_SANDBOX_ID` is still accepted as a fallback for existing `.env` files.

## Feature overview

Expand Down Expand Up @@ -85,7 +90,7 @@ If your agent publishes a video track (for example via a [virtual avatar](https:

In a production environment, you will be responsible for developing a solution to [generate tokens for your users](https://docs.livekit.io/home/server/generating-tokens/) that integrates with your authentication system.

You should replace the `SandboxTokenSource` in `lib/controllers/app_ctrl.dart` with an `EndpointTokenSource` or your own `TokenSourceFixed` / `TokenSourceConfigurable` implementation. You can also use `.cached()` to cache valid tokens and avoid unnecessary token requests.
You should replace the `DevelopmentTokenSource` in `lib/controllers/app_ctrl.dart` with an `EndpointTokenSource` or your own `TokenSourceFixed` / `TokenSourceConfigurable` implementation. You can also use `.cached()` to cache valid tokens and avoid unnecessary token requests.

## Running on Simulator / Emulator

Expand Down
11 changes: 8 additions & 3 deletions lib/controllers/app_ctrl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,17 @@ class AppCtrl extends ChangeNotifier {
);
}

final sandboxId = dotenv.env['LIVEKIT_SANDBOX_ID']?.replaceAll('"', '');
// The development token server ID from your LiveKit Cloud project's Settings page.
// LIVEKIT_SANDBOX_ID is the former name of this setting and is still accepted.
final tokenServerId =
(dotenv.env['LIVEKIT_TOKEN_SERVER_ID'] ?? dotenv.env['LIVEKIT_SANDBOX_ID'])?.replaceAll('"', '');
const placeholderIds = {'<your-token-server-id>', '<your-sandbox-id>'};
Comment on lines +54 to +55

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes it backwards compatible


sdk.EndpointTokenSource tokenSource;
if (sandboxId == null || sandboxId.isEmpty || sandboxId == '<your-sandbox-id>') {
if (tokenServerId == null || tokenServerId.isEmpty || placeholderIds.contains(tokenServerId)) {
tokenSource = sdk.EndpointTokenSource(url: Uri.parse(homepageAgentTokenEndpoint));
} else {
tokenSource = sdk.SandboxTokenSource(sandboxId: sandboxId);
tokenSource = sdk.DevelopmentTokenSource(id: tokenServerId);
}

return sdk.Session.fromConfigurableTokenSource(
Expand Down
2 changes: 1 addition & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'app.dart';

// Load environment variables before starting the app
// This is used to configure the LiveKit sandbox ID for development
// This is used to configure the LiveKit development token server ID
// The file is optional; without it the app connects to a default agent (see app_ctrl.dart)
void main() async {
await dotenv.load(fileName: 'assets/.env', isOptional: true);
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ dependencies:
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.8
chat_bubbles: ^1.6.0
livekit_client: ^2.10.0
livekit_client: ^2.11.0
flutter_dotenv: ^6.0.0
http: ^1.3.0
provider: ^6.1.2
Expand Down
2 changes: 1 addition & 1 deletion test/widget_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ void main() {
fileName: 'assets/.env',
isOptional: true,
mergeWith: const {
'LIVEKIT_SANDBOX_ID': 'test',
'LIVEKIT_TOKEN_SERVER_ID': 'test',
},
);

Expand Down
Loading