I want to add userid to metadata in my iOS swift c...
# ask-ai
t
I want to add userid to metadata in my iOS swift code. Give me an example.
t
Thanks for reaching out, Ishita. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hey there! If you want to add a user ID to metadata in your iOS Swift code for LiveKit, you can easily do that using the
set(metadata:)
method on the
LocalParticipant
class. Here’s a simple example to get you started:
Copy code
let userId = "user123"
let metadata = ["userId": userId]
let metadataString = try? JSONEncoder().encode(metadata)
let jsonString = String(data: metadataString!, encoding: .utf8)

do {
    try await room.localParticipant.set(metadata: jsonString ?? "")
} catch {
    print("Failed to set metadata: \(error)")
}
Just a heads-up: make sure the participant’s access token has the
canUpdateOwnMetadata
permission. Without it, the update won’t go through. Once set, this metadata will sync with everyone else in the room and even be available to new folks who join later on. Also, a quick note — the metadata field has a 64 KiB size limit, so keep an eye on your data size to avoid any hiccups. Sources: Participant attributes and metadata | LiveKit Docs | Sources/LiveKit/Broadcast/Support/OSLogHandler.swift
t
How do I request canUpdateOwnMetadata permission when creating participant’s access token on iOS?
t
Just a quick heads-up: the
canUpdateOwnMetadata
permission needs to be included in the participant’s access token when it's created on your server. Since the iOS SDK doesn’t generate tokens, you’ll want to handle this part in your backend. Here’s a simple example using the Node.js server SDK to get you started:
Copy code
import { AccessToken } from 'livekit-server-sdk';

const at = new AccessToken('API_KEY', 'API_SECRET', {
  identity: 'user123',
  ttl: '1h',
});

at.addGrant({
  roomJoin: true,
  room: 'my-room',
  canUpdateOwnMetadata: true, // This lets users update their own metadata
});

const token = await at.toJwt();
// You can then send this token to your iOS app
Just make sure the
canUpdateOwnMetadata
flag is set to
true
in the video grant—this is what lets participants update their own metadata, attributes, or name once they’ve joined the room. If you’re working with a different server SDK like Python, Kotlin, or Go, don’t worry—there’s similar support for setting this permission. Feel free to reach out if you want a hand with your backend setup! Sources: packages/livekit-server-sdk/src/grants.ts | Sources/LiveKit/Participant/LocalParticipant.swift | src/room/participant/LocalParticipant.ts | Authentication Examples | livekit/livekit_composite | DeepWiki | TEMPLATE.md
Hey there! To add the
canUpdateOwnMetadata
permission in your Python backend, you just need to include it in the
VideoGrants
object when you’re generating the access token. Here’s a quick example to help you out:
Copy code
from livekit.api import AccessToken, VideoGrants

# Create access token
token = AccessToken(api_key="your-api-key", api_secret="your-api-secret")

# Create video grants with canUpdateOwnMetadata permission
grants = VideoGrants(
    room_join=True,
    room="your-room-name",
    can_update_own_metadata=True,  # This lets participants update their own metadata
    can_publish=True,
    can_subscribe=True,
    can_publish_data=True
)

# Set the grants and participant identity
token.with_grants(grants).with_identity("participant-identity")

# Generate the JWT token
jwt_token = token.to_jwt()
Just make sure to include this part in your backend where you create access tokens. The important bit is setting
can_update_own_metadata=True
inside the
VideoGrants
object. This permission lets participants update their own metadata, like when using the
setMetadata
method in the iOS SDK. If you want, I’m here to help you fit this into your specific backend setup—just let me know! Sources: Participant attributes and metadata | LiveKit Docs | livekit-rtc/livekit/rtc/participant.py | Authentication | LiveKit Docs