Initialize RoomServiceClient
Participant management is done with a RoomServiceClient, created like so:
import (lksdk "github.com/livekit/server-sdk-go"livekit "github.com/livekit/protocol/livekit")// ...host := "https://my.livekit.host"roomClient := lksdk.NewRoomServiceClient(host, "api-key", "secret-key")
List Participants
res, err := roomClient.ListParticipants(context.Background(), &livekit.ListParticipantsRequest{Room: roomName,})
Get details on a Participant
res, err := roomClient.GetParticipant(context.Background(), &livekit.RoomParticipantIdentity{Room: roomName,Identity: identity,})
Updating permissions
You can modify a participant's permissions on-the-fly using UpdateParticipant
. When there's a change in permissions, connected clients will be notified through the ParticipantPermissionChanged
event.
This comes in handy, for instance, when transitioning an audience member to a speaker role within a room.
Note that if you revoke the CanPublish
permission from a participant, all tracks they've published will be automatically unpublished.
// Promotes an audience member to a speakerres, err := c.UpdateParticipant(context.Background(), &livekit.UpdateParticipantRequest{Room: roomName,Identity: identity,Permission: &livekit.ParticipantPermission{CanSubscribe: true,CanPublish: true,CanPublishData: true,},})// ...and later move them back to audienceres, err := c.UpdateParticipant(context.Background(), &livekit.UpdateParticipantRequest{Room: roomName,Identity: identity,Permission: &livekit.ParticipantPermission{CanSubscribe: true,CanPublish: false,CanPublishData: true,},})
Updating metadata
You can modify a Participant's metadata whenever necessary. Once changed, connected clients will receive a ParticipantMetadataChanged
event.
data, err := json.Marshal(values)_, err = c.UpdateParticipant(context.Background(), &livekit.UpdateParticipantRequest{Room: roomName,Identity: identity,Metadata: string(data),})
Remove a Participant
RemoteParticipant
will forcibly disconnect the participant from the room. However, this action doesn't invalidate the participant's token.
To prevent the participant from rejoining the same room, consider the following measures:
- Generate access tokens with a short TTL (Time-To-Live).
- Refrain from providing a new token to the same participant via your application's backend.
res, err := roomClient.RemoveParticipant(context.Background(), &livekit.RoomParticipantIdentity{Room: roomName,Identity: identity,})
Mute/unmute a Participant's Track
To mute a particular Track from a Participant, first get the TrackSid from GetParticipant
(above), then call MutePublishedTrack
:
res, err := roomClient.MutePublishedTrack(context.Background(), &livekit.MuteRoomTrackRequest{Room: roomName,Identity: identity,TrackSid: "track_sid",Muted: true,})
You may also unmute the track by setting muted
to false
.
Being remotely unmuted can catch users by surprise, so it's disabled by default.
To allow remote unmute, select the Admins can remotely unmute tracks
option in your project settings.
If you're self-hosting, configure room.enable_remote_unmute: true
in your config YAML.