Skip to content

Commit 8e37fed

Browse files
Adding support for room join with attributes (#553)
* feat(room): add attribute args * feat: add support for loading participant attributes from JSON file This change adds the ability to read participant attributes from a JSON file when joining a room, providing a more convenient way to specify multiple attributes compared to using separate --attribute flags on the command line. * chore: add nanpa changeset adding nanpa chngeset for a new feature room join with attributes * chore: restore missing test files
1 parent 46e9d38 commit 8e37fed

File tree

3 files changed

+70
-4
lines changed

3 files changed

+70
-4
lines changed

.nanpa/room-join-attr.kdl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
minor type="added" "Added room joining with setting attributes and attribute file"

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,32 @@ See the [LiveKit Templates Index](https://github.com/livekit-examples/index?tab=
107107

108108
## Publishing to a room
109109

110+
### Join a room and set participant attributes
111+
112+
```shell
113+
lk room join --identity publisher \
114+
--attribute key1=value1 \
115+
--attribute key2=value2 \
116+
<room_name>
117+
```
118+
119+
You can also specify attributes by providing a JSON file:
120+
121+
```shell
122+
lk room join --identity publisher \
123+
--attribute-file attributes.json \
124+
<room_name>
125+
```
126+
127+
Where `attributes.json` contains a JSON object with key-value pairs:
128+
129+
```json
130+
{
131+
"key1": "value1",
132+
"key2": "value2"
133+
}
134+
```
135+
110136
### Publish demo video track
111137

112138
To publish a demo video as a participant's track, use the following:

cmd/lk/room.go

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,15 @@ var (
168168
Name: "exit-after-publish",
169169
Usage: "When publishing, exit after file or stream is complete",
170170
},
171+
&cli.StringSliceFlag{
172+
Name: "attribute",
173+
Usage: "set attributes in key=value format, can be used multiple times",
174+
},
175+
&cli.StringFlag{
176+
Name: "attribute-file",
177+
Usage: "read attributes from a `JSON` file",
178+
TakesFile: true,
179+
},
171180
},
172181
},
173182
{
@@ -863,11 +872,41 @@ func joinRoom(ctx context.Context, cmd *cli.Command) error {
863872
close(done)
864873
},
865874
}
875+
876+
participantAttributes := make(map[string]string)
877+
878+
attrs := cmd.StringSlice("attribute")
879+
for _, attr := range attrs {
880+
kv := strings.Split(attr, "=")
881+
if len(kv) == 2 {
882+
participantAttributes[kv[0]] = kv[1]
883+
}
884+
}
885+
886+
// Read attributes from JSON file if specified
887+
if attrFile := cmd.String("attribute-file"); attrFile != "" {
888+
fileData, err := os.ReadFile(attrFile)
889+
if err != nil {
890+
return fmt.Errorf("failed to read attribute file: %w", err)
891+
}
892+
893+
var fileAttrs map[string]string
894+
if err := json.Unmarshal(fileData, &fileAttrs); err != nil {
895+
return fmt.Errorf("failed to parse attribute file as JSON: %w", err)
896+
}
897+
898+
// Add attributes from file to the existing ones
899+
for key, value := range fileAttrs {
900+
participantAttributes[key] = value
901+
}
902+
}
903+
866904
room, err := lksdk.ConnectToRoom(pc.URL, lksdk.ConnectInfo{
867-
APIKey: pc.APIKey,
868-
APISecret: pc.APISecret,
869-
RoomName: roomName,
870-
ParticipantIdentity: participantIdentity,
905+
APIKey: pc.APIKey,
906+
APISecret: pc.APISecret,
907+
RoomName: roomName,
908+
ParticipantIdentity: participantIdentity,
909+
ParticipantAttributes: participantAttributes,
871910
}, roomCB)
872911
if err != nil {
873912
return err

0 commit comments

Comments
 (0)