carlos / platform Public

docs: live streams on your own LiveKit Cloud project

A guide for app builders who bring their own LiveKit key: tokens for
streamer and viewers, chat over data messages, a room composite or track
Egress into the app's store, the egress_ended webhook, then media
conversion and playback.

Platform facts it rests on were checked in code: public egress needs no
allow, webhooks need a host outside carlos gate, and an upload must name
the store's region and carry no tags. Two gaps are listed under Not yet
supported: no write-only credential for a third party, and no conversion
past 2 hours.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 93d5c514c89bab0e6566d147e9ce7a9911ea9d95 parent 65951fb
2 files changed, +240 −0
  • docs/site/live-streams-bring-your-own-livekit.md +235 −0
  • docs/site/nav.json +5 −0
diff --git a/docs/site/live-streams-bring-your-own-livekit.md b/docs/site/live-streams-bring-your-own-livekit.md
new file mode 100644
index 000000000..429fb95a7
--- /dev/null
+++ b/docs/site/live-streams-bring-your-own-livekit.md
@@ -0,0 +1,235 @@
+# Live streams with your own LiveKit
+
+Your app can run a live stream with chat on your own [LiveKit Cloud](https://livekit.io) project. One person streams, viewers watch and chat, and LiveKit records the stream into your app's own store. Then your app converts the recording with [media conversion](/docs/media-conversion) and plays it back like any other upload.
+
+The platform does not run LiveKit for you here. Your app holds its own LiveKit key, talks to LiveKit Cloud directly, and LiveKit bills your own LiveKit account.
+
+## Before you start
+
+You need:
+
+- A LiveKit Cloud project. Its settings page gives you the project URL, such as `wss://shop-a1b2c3.livekit.cloud`, and an API key and secret.
+- An object store for the app, made with `carlos store create --app shop`, on a deployment with media conversion turned on. The store must pass the check on the media conversion page. A store made with `carlos store create` and left alone passes.
+- A Service credential for media conversion, as in [the media conversion example](/docs/media-conversion#an-example).
+
+What the platform needs from your app:
+
+- **No egress setting.** Instances can already reach public addresses, and LiveKit Cloud's API and project hosts are public. `carlos apps set --egress-allow` is only for private addresses, and it takes IP addresses, not hostnames.
+- **A public route for LiveKit's webhook.** LiveKit calls your app when a recording is done. Any path on your app's own hostname works, such as `https://shop.example.com/livekit/webhook`. A host behind [carlos gate](/docs/cli#carlos-gate) can't receive it, because the gate asks every request for the password.
+- **The store's region in every recording request.** If you leave the region out, LiveKit asks S3 for the bucket's location, and the store's credential isn't allowed to ask.
+- **No tags on recordings.** LiveKit can tag the files it uploads. Leave that empty: the store's credential can't tag objects, so S3 refuses the upload.
+
+## How it fits together
+
+```text
+# once
+carlos env set --app shop LIVEKIT_URL=wss://shop-a1b2c3.livekit.cloud
+carlos secrets set --app shop LIVEKIT_API_KEY=APIa1b2c3d4
+carlos secrets set --app shop LIVEKIT_API_SECRET - < livekit-secret.txt
+add https://shop.example.com/livekit/webhook as a webhook in the LiveKit project
+
+# per stream, on the app's server
+1. mint a token for the streamer and one for each viewer -> the browsers join the room
+2. start an Egress to the app's store -> LiveKit records the room
+3. LiveKit POSTs egress_ended to /livekit/webhook -> the recording's key
+4. POST the key to media conversion, poll until done -> the files to play
+```
+
+The API key and secret are secrets, so they go in with `carlos secrets set`. The `-` reads the secret from the file, so it never appears on your command line. The project URL isn't secret. Your browser code needs it too.
+
+The examples below are in Go, with LiveKit's server SDK (`github.com/livekit/server-sdk-go/v2`) and its protocol package (`github.com/livekit/protocol`). LiveKit's other server SDKs have the same calls.
+
+## Tokens for the streamer and the viewers
+
+A browser joins a LiveKit room with a token your server signs. Every token names the room, a participant identity and what that participant may do. Use your own user ID as the identity, so each person appears once.
+
+```go
+import (
+ "os"
+ "time"
+
+ "github.com/livekit/protocol/auth"
+)
+
+// joinToken lets one person into a stream's room. The streamer may publish
+// a camera and microphone; viewers only watch. Everyone may chat.
+func joinToken(room, identity, name string, streamer bool) (string, error) {
+ grant := &auth.VideoGrant{RoomJoin: true, Room: room}
+ grant.SetCanSubscribe(true)
+ grant.SetCanPublish(streamer)
+ grant.SetCanPublishData(true)
+ return auth.NewAccessToken(os.Getenv("LIVEKIT_API_KEY"), os.Getenv("LIVEKIT_API_SECRET")).
+ SetVideoGrant(grant).
+ SetIdentity(identity).
+ SetName(name).
+ SetValidFor(10 * time.Minute).
+ ToJWT()
+}
+```
+
+Set the chat permission even for viewers. If you leave it out, LiveKit gives each participant the same chat permission as their publish permission, so viewers couldn't chat.
+
+Your server decides who is the streamer. Mint a streamer token only for the person who owns the stream, and viewer tokens for everyone else allowed to watch. The token only has to be valid when the browser connects. LiveKit keeps a connected browser's session going after that.
+
+## Chat
+
+Chat travels as LiveKit data messages inside the room. In the browser, the LiveKit client sends a message with `room.localParticipant.publishData(bytes, { reliable: true })` and receives messages through the `RoomEvent.DataReceived` event. Send each message as a small JSON object, such as `{"text": "hello"}`. One reliable message can carry up to 15 KiB.
+
+LiveKit doesn't store data messages. A viewer who joins late sees only the messages sent after they joined. If you want chat history or a record for moderation, have the browser also send each message to your server, and keep it there.
+
+To stop someone chatting, your server calls `UpdateParticipant` on LiveKit's room service with `CanPublishData` set to false. To remove them from the stream, it calls `RemoveParticipant`. Both are on `lksdk.NewRoomServiceClient`.
+
+## Recording into your store
+
+Your server starts an Egress, LiveKit's recorder, and points it at the app's store with the store's own credentials. The platform delivers them to every instance as `CARLOS_STORE_BUCKET`, `CARLOS_STORE_REGION`, `CARLOS_STORE_ACCESS_KEY` and `CARLOS_STORE_SECRET_KEY`.
+
+A room composite Egress records the room as one MP4, laid out the way a viewer sees it:
+
+```go
+import (
+ "context"
+ "os"
+
+ "github.com/livekit/protocol/livekit"
+ lksdk "github.com/livekit/server-sdk-go/v2"
+)
+
+func storeUpload() *livekit.S3Upload {
+ return &livekit.S3Upload{
+ AccessKey: os.Getenv("CARLOS_STORE_ACCESS_KEY"),
+ Secret: os.Getenv("CARLOS_STORE_SECRET_KEY"),
+ Region: os.Getenv("CARLOS_STORE_REGION"), // required: see Before you start
+ Bucket: os.Getenv("CARLOS_STORE_BUCKET"),
+ // No Tagging. The store refuses tagged uploads.
+ }
+}
+
+// startRecording records the whole room under recordings/<id>/.
+func startRecording(ctx context.Context, room, id string) (string, error) {
+ egress := lksdk.NewEgressClient(os.Getenv("LIVEKIT_URL"), os.Getenv("LIVEKIT_API_KEY"), os.Getenv("LIVEKIT_API_SECRET"))
+ info, err := egress.StartRoomCompositeEgress(ctx, &livekit.RoomCompositeEgressRequest{
+ RoomName: room,
+ Layout: "speaker",
+ FileOutputs: []*livekit.EncodedFileOutput{{
+ FileType: livekit.EncodedFileType_MP4,
+ Filepath: "recordings/" + id + "/",
+ Output: &livekit.EncodedFileOutput_S3{S3: storeUpload()},
+ }},
+ })
+ if err != nil {
+ return "", err
+ }
+ return info.EgressId, nil
+}
+```
+
+`Filepath` ends in `/`, so LiveKit names the file itself inside that folder, as `<room>-<time>.mp4`. That also keeps a retried recording from overwriting the first try. Don't record under `derived/`: media conversion won't take a source there.
+
+A track Egress records one track as it arrives, without re-encoding it. LiveKit picks the file type from the track's codec: WebM, Ogg or MP4. Media conversion reads all three. Use it when you only need the streamer's camera or microphone:
+
+```go
+info, err := egress.StartTrackEgress(ctx, &livekit.TrackEgressRequest{
+ RoomName: room,
+ TrackId: trackID,
+ Output: &livekit.TrackEgressRequest_File{File: &livekit.DirectFileOutput{
+ Filepath: "recordings/" + id + "/",
+ Output: &livekit.DirectFileOutput_S3{S3: storeUpload()},
+ }},
+})
+```
+
+LiveKit uploads the file when the recording stops. It also writes a small JSON manifest beside the file. Set `DisableManifest: true` on the output if you don't want it.
+
+If your app also gets `CARLOS_STORE_ENDPOINT`, your deployment's store isn't on AWS. Pass that value as `Endpoint` and set `ForcePathStyle: true`. LiveKit has to be able to reach that endpoint over the internet.
+
+## When the recording is done
+
+LiveKit POSTs events to the webhook URL you set in the LiveKit project, with `Content-Type: application/webhook+json`. Each one carries a token signed with your API secret, and the SDK checks it for you. The one you need is `egress_ended`:
+
+```go
+import (
+ "net/http"
+ "os"
+ "time"
+
+ "github.com/livekit/protocol/auth"
+ "github.com/livekit/protocol/livekit"
+ "github.com/livekit/protocol/webhook"
+)
+
+func livekitWebhook(w http.ResponseWriter, r *http.Request) {
+ keys := auth.NewSimpleKeyProvider(os.Getenv("LIVEKIT_API_KEY"), os.Getenv("LIVEKIT_API_SECRET"))
+ event, err := webhook.ReceiveWebhookEvent(r, keys)
+ if err != nil {
+ http.Error(w, "signature does not verify", http.StatusUnauthorized)
+ return
+ }
+ if event.GetEvent() == webhook.EventEgressEnded {
+ info := event.GetEgressInfo()
+ switch {
+ case info.GetBackupStorageUsed():
+ // The upload to your store failed. See "When an upload fails".
+ case info.GetStatus() == livekit.EgressStatus_EGRESS_COMPLETE,
+ info.GetStatus() == livekit.EgressStatus_EGRESS_LIMIT_REACHED:
+ for _, f := range info.GetFileResults() {
+ queueConversion(f.GetFilename(), time.Duration(f.GetDuration()))
+ }
+ default:
+ // EGRESS_FAILED: info.GetError() says why.
+ }
+ }
+ w.WriteHeader(http.StatusOK)
+}
+```
+
+`Filename` is the recording's key in your store, such as `recordings/a1b2/stream-a1b2-2026-11-25T101500.mp4`. `Duration` is in nanoseconds. `EGRESS_LIMIT_REACHED` means LiveKit stopped a long recording at its time limit, and the file up to that point is still in your store.
+
+LiveKit retries a webhook a few times and may send one more than once. Make the handler safe to run twice. Submitting the same key to media conversion twice is already safe: the second `POST` returns the first job.
+
+## Converting and playing the recording
+
+Send the recording's key to media conversion, as on the [media conversion page](/docs/media-conversion#starting-a-conversion):
+
+```http
+POST /api/cli/apps/{account}/shop/media/conversions
+Authorization: Bearer <service token>
+{"key": "recordings/a1b2/stream-a1b2-2026-11-25T101500.mp4", "class": "long"}
+```
+
+Ask for `"class": "long"` when the recording is longer than 10 minutes. A short job converts up to 10 minutes and fails anything longer with `needs_long_class`, and most streams run longer than that. A long job converts up to 2 hours of video up to 1080p. A room composite recording is 720p at 30 frames a second, with Opus sound, unless you ask for other encoding options. Conversion reads both.
+
+Poll the job until it is `done`, save the output keys with the stream, and serve them as the page's [Serving](/docs/media-conversion#serving) section describes: the playlist and the MP4 from your app's own origin, side by side.
+
+Conversion counts against your account's [media allowance](/docs/media-conversion#the-allowance) like any other video.
+
+## When an upload fails
+
+If LiveKit can't upload the recording to your store, the recording isn't in your store and there is nothing to convert. Two common causes:
+
+- **The store is full.** A store past its plan's storage allowance refuses new objects, recordings included.
+- **The credential changed while the stream ran.** LiveKit uses the credential it was given when the recording started, and uploads at the end. If you run `carlos store rotate --finish` during a stream, the old credential stops working and the upload fails. Finish a rotation only when no recording is running.
+
+The webhook then reports `EGRESS_FAILED`, and its error says the upload failed.
+
+LiveKit Cloud can keep a copy of a recording whose upload failed in its own backup storage for 7 days. LiveKit turns this on for a project when you ask. When it happens, the webhook has `backup_storage_used` set, and the file is at LiveKit, not in your store.
+
+## Virus scanning
+
+If your store has [virus scanning](/docs/cli#carlos-store-scan), a recording is scanned like any other new object. Most recordings are bigger than the scanner's size limit, 25 MiB unless your operator changed it, so they are counted as skipped, not scanned. Media conversion doesn't wait for a scan. It only refuses an object a scan has found infected while the store enforces scanning.
+
+## Privacy: a third party in the media path
+
+With this setup, LiveKit Cloud carries every stream. The video, the audio and the chat pass through LiveKit's servers, and LiveKit records the stream before it reaches your store. LiveKit also holds your store's credential while a recording runs. That credential can read and change everything in your app's store, not only recordings.
+
+The platform doesn't tell your users any of this. Whether and how to tell them is your app's decision. If you do, say it where people decide to stream or watch: near the start of your app's description, and on anything the stream produces, such as the recording's page.
+
+## Billing
+
+LiveKit bills your own LiveKit account for everything LiveKit does: participant minutes, data sent out of LiveKit to viewers, and Egress recording time. Check LiveKit's pricing page for current prices and free allowances. A room composite recording is billed as transcoding minutes. A track recording is billed separately, at a much lower rate.
+
+The platform bills your account as usual: the instance, the recording's storage in your store, and the media minutes conversion uses.
+
+## Not yet supported
+
+- **A credential that can only write recordings.** LiveKit needs your store's one credential, which can also read, overwrite and delete every other object in the store. The platform can't yet give an app a narrower credential to hand to a third party.
+- **Converting recordings longer than 2 hours.** LiveKit Cloud can record a single file for up to 3 hours, and media conversion converts up to 2. To keep a longer stream playable, stop the recording and start a new one before it reaches 2 hours. Each part converts on its own.
diff --git a/docs/site/nav.json b/docs/site/nav.json
index ec8ce392c..9da2d31cd 100644
--- a/docs/site/nav.json
+++ b/docs/site/nav.json
@@ -48,6 +48,11 @@
"label": "Media conversion",
"blurb": "Turn an uploaded video or audio file into files for browser playback, and serve them from your app."
},
+ {
+ "slug": "live-streams-bring-your-own-livekit",
+ "label": "Live streams with your own LiveKit",
+ "blurb": "Stream with chat on your own LiveKit Cloud project, record into your store, and play the recording back."
+ },
{
"slug": "scan-callbacks",
"label": "Receiving scan results",