Skip to content

Commit a3279d7

Browse files
[Communication][Rooms] throttle participant operations in samples to avoid conflicts (Azure#25762)
### Packages impacted by this PR ### Issues associated with this PR ### Describe the problem that is addressed by this PR ### What are the possible designs available to address the problem? If there are more than one possible design, why was the one in this PR chosen? ### Are there test cases added in this PR? _(If not, why?)_ ### Provide a list of related PRs _(if any)_ ### Command used to generate this PR:**_(Applicable only to SDK release request PRs)_ ### Checklists - [ ] Added impacted package name to the issue description - [ ] Does this PR needs any fixes in the SDK Generator?** _(If so, create an Issue in the [Autorest/typescript](https://github.com/Azure/autorest.typescript) repository and link it here)_ - [ ] Added a changelog (if necessary)
1 parent ce5e752 commit a3279d7

File tree

10 files changed

+272
-123
lines changed

10 files changed

+272
-123
lines changed

sdk/communication/communication-rooms/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ const roomsClient: RoomsClient = new RoomsClient(CONNECTION_STRING);
9393

9494
const validFrom = new Date(Date.now());
9595
let validForDays = 10;
96-
let validUntil = validFrom.setDate(validFrom.getTime() + validForDays);
96+
let validUntil = new Date(validFrom.getTime());
97+
validUntil.setDate(validFrom.getDate() + validForDays);
9798

9899
// options payload to create a room
99100
const createRoomOptions: CreateRoomOptions = {
@@ -119,7 +120,7 @@ To update the `validFrom` and `validUntil` settings of a room use the `updateRoo
119120

120121
```js
121122
validForDays = 60;
122-
validUntil = validFrom.setDate(validFrom.getDate() + validForDays);
123+
validUntil.setDate(validFrom.getDate() + validForDays);
123124
const updateRoomOptions: UpdateRoomOptions = {
124125
validFrom,
125126
validUntil,

sdk/communication/communication-rooms/samples-dev/participantOperations.ts

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ import { PagedAsyncIterableIterator } from "@azure/core-paging";
1919
dotenv.config();
2020

2121
export async function main() {
22+
console.log("Room Participant Operations JavaScript Sample");
23+
console.log("_________________________________\n");
24+
2225
const connectionString =
2326
process.env["COMMUNICATION_SAMPLES_CONNECTION_STRING"] ||
2427
"endpoint=https://<resource-name>.communication.azure.com/;<access-key>";
@@ -27,6 +30,8 @@ export async function main() {
2730
const user1 = await identityClient.createUserAndToken(["voip"]);
2831
const user2 = await identityClient.createUserAndToken(["voip"]);
2932

33+
console.log("Creating room...");
34+
3035
// create RoomsClient
3136
const roomsClient: RoomsClient = new RoomsClient(connectionString);
3237

@@ -48,7 +53,9 @@ export async function main() {
4853
// create a room with the request payload
4954
const createRoom = await roomsClient.createRoom(createRoomOptions);
5055
const roomId = createRoom.id;
51-
console.log(`Created Room with ID ${roomId}`);
56+
console.log(`Successfully created room with id: ${roomId}.`);
57+
58+
console.log(`Adding new participant to room with id: ${roomId}...`);
5259

5360
// request payload to add participants
5461
const addParticipantsList: RoomParticipantPatch[] = [
@@ -60,9 +67,16 @@ export async function main() {
6067

6168
// add user2 to the room with the request payload
6269
await roomsClient.addOrUpdateParticipants(roomId, addParticipantsList);
63-
const addedParticipants = await roomsClient.listParticipants(roomId);
64-
console.log(`Added Participants`);
65-
printParticipants(addedParticipants);
70+
71+
console.log(`Successfully added participant to room with id: ${roomId}.`);
72+
console.log("Printing participants in room...");
73+
74+
let participantsInRoom = await roomsClient.listParticipants(roomId);
75+
await printParticipants(participantsInRoom);
76+
77+
await pause(500);
78+
79+
console.log("Updating role of participant...");
6680

6781
// request payload to update user1 with a new role
6882
const updateParticipantsList: RoomParticipantPatch[] = [
@@ -74,34 +88,60 @@ export async function main() {
7488

7589
// update user1 with the request payload
7690
await roomsClient.addOrUpdateParticipants(roomId, updateParticipantsList);
77-
console.log(`Updated Participants`);
78-
printParticipants(await roomsClient.listParticipants(roomId));
91+
console.log(`Successfully updated participant in room with id: ${roomId}.`);
92+
console.log("Printing updated participants in room...");
93+
94+
participantsInRoom = await roomsClient.listParticipants(roomId);
95+
await printParticipants(participantsInRoom);
96+
97+
await pause(500);
98+
99+
console.log("Removing participant from room...");
79100

80101
// request payload to delete both users from the room
81102
// this demonstrates both objects that can be used in deleting users from rooms: RoomParticipant or CommunicationIdentifier
82103
const removeParticipantsList = [user1.user, user2.user];
83104

84105
// remove both users from the room with the request payload
85106
await roomsClient.removeParticipants(roomId, removeParticipantsList);
86-
console.log(`Removed Participants`);
87-
printParticipants(await roomsClient.listParticipants(roomId));
107+
console.log(`Successfully removed participant from room with id: ${roomId}.`);
108+
109+
participantsInRoom = await roomsClient.listParticipants(roomId);
110+
await printParticipants(participantsInRoom);
111+
112+
await pause(500);
113+
114+
console.log(`Deleting room with id: ${roomId}...`);
88115

89116
// deletes the room for cleanup
90117
await roomsClient.deleteRoom(roomId);
118+
119+
console.log(`Successfully deleted room with id: ${roomId}.`);
91120
}
92121

93122
/**
94123
* Outputs the participants within a Participantsn to console.
95-
* @param pc - The Participants being printed to console.
124+
* @param participants - The Participants being printed to console.
96125
*/
97126
async function printParticipants(
98127
participants: PagedAsyncIterableIterator<Partial<RoomParticipant>>
99128
): Promise<void> {
129+
var count = 0;
100130
for await (const participant of participants) {
101-
const role = participant.role;
102-
console.log(role);
131+
if (participant) {
132+
count++;
133+
const { role, id } = participant;
134+
console.log(`---Participant ${count}---`);
135+
console.log(`Kind: ${id?.kind}`);
136+
console.log(`Role: ${role}`);
137+
}
103138
}
104139
}
140+
141+
async function pause(time: number): Promise<void> {
142+
await new Promise((resolve) => setTimeout(resolve, time));
143+
}
144+
105145
main().catch((error) => {
106146
console.error("Encountered an error while sending request: ", error);
107147
process.exit(1);

sdk/communication/communication-rooms/samples-dev/roomOperations.ts

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,24 @@ import * as dotenv from "dotenv";
1818
dotenv.config();
1919

2020
export async function main() {
21+
console.log("Room Operations JavaScript Sample");
22+
console.log("_________________________________\n\n");
23+
2124
const connectionString =
2225
process.env["COMMUNICATION_SAMPLES_CONNECTION_STRING"] ||
2326
"endpoint=https://<resource-name>.communication.azure.com/;<access-key>";
2427

2528
const identityClient = new CommunicationIdentityClient(connectionString);
2629
const user1 = await identityClient.createUserAndToken(["voip"]);
2730

31+
console.log("Creating room...");
32+
2833
// create RoomsClient
2934
const roomsClient: RoomsClient = new RoomsClient(connectionString);
3035

3136
var validFrom = new Date(Date.now());
32-
var validUntil = new Date(validFrom.getTime() + 5 * 60 * 1000);
37+
var validForDays = 10;
38+
var validUntil = addDays(validFrom, validForDays);
3339

3440
// options payload to create a room
3541
const createRoomOptions: CreateRoomOptions = {
@@ -46,30 +52,35 @@ export async function main() {
4652
// create a room with the request payload
4753
const createRoom = await roomsClient.createRoom(createRoomOptions);
4854
const roomId = createRoom.id;
49-
console.log(`Created Room`);
55+
console.log("Successfully created room with following properties:");
5056
printRoom(createRoom);
5157

58+
console.log(`Fetching room with id: ${roomId}...`);
59+
5260
// retrieves the room with corresponding ID
5361
const getRoom = await roomsClient.getRoom(roomId);
54-
console.log(`Retrieved Room with ID ${roomId}`);
62+
console.log(`Successfully fetched room with id: ${roomId}. Room details:`);
5563
printRoom(getRoom);
5664

57-
validFrom.setTime(validUntil.getTime());
58-
validUntil.setTime(validFrom.getTime() + 5 * 60 * 1000);
65+
console.log(`Updating room with id: ${roomId}...`);
5966

6067
// request payload to update a room
6168
const updateRoomOptions: UpdateRoomOptions = {
6269
validFrom,
63-
validUntil,
70+
validUntil: addDays(validUntil, 10),
6471
};
6572

6673
// updates the specified room with the request payload
6774
const updateRoom = await roomsClient.updateRoom(roomId, updateRoomOptions);
68-
console.log(`Updated Room`);
75+
console.log(`Successfully updated room with id: ${roomId}. Room details:`);
6976
printRoom(updateRoom);
7077

78+
console.log(`Deleting room with id: ${roomId}...`);
79+
7180
// deletes the specified room
7281
await roomsClient.deleteRoom(roomId);
82+
83+
console.log(`Successfully deleted room with id: ${roomId}.`);
7384
}
7485

7586
/**
@@ -79,7 +90,18 @@ export async function main() {
7990
function printRoom(room: CommunicationRoom): void {
8091
console.log(`Room ID: ${room.id}`);
8192
console.log(`Valid From: ${room.validFrom}`);
82-
console.log(`Valid Until: ${room.validUntil}`);
93+
console.log(`Valid Until: ${room.validUntil}\n\n`);
94+
}
95+
96+
/**
97+
* Adds the specified ampunt of days
98+
* @param current - The original date
99+
* @param days - The number of days to add
100+
*/
101+
function addDays(current: Date, days: number): Date {
102+
const copied = new Date(current.getTime());
103+
copied.setDate(copied.getDate() + days);
104+
return copied;
83105
}
84106

85107
main().catch((error) => {

sdk/communication/communication-rooms/samples/v1-beta/javascript/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@
3232
"@azure/communication-rooms": "next",
3333
"dotenv": "latest",
3434
"@azure/communication-identity": "^1.1.0-beta.2",
35-
"@azure/communication-common": "^2.1.0"
35+
"@azure/core-paging": "^1.5.0"
3636
}
3737
}

sdk/communication/communication-rooms/samples/v1-beta/javascript/participantOperations.js

Lines changed: 55 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77

88
const { RoomsClient } = require("@azure/communication-rooms");
99
const { CommunicationIdentityClient } = require("@azure/communication-identity");
10-
const { getIdentifierRawId } = require("@azure/communication-common");
1110

1211
// Load the .env file if it exists
1312
require("dotenv").config();
1413

1514
async function main() {
15+
console.log("Room Participant Operations JavaScript Sample");
16+
console.log("_________________________________\n");
17+
1618
const connectionString =
1719
process.env["COMMUNICATION_SAMPLES_CONNECTION_STRING"] ||
1820
"endpoint=https://<resource-name>.communication.azure.com/;<access-key>";
@@ -21,6 +23,8 @@ async function main() {
2123
const user1 = await identityClient.createUserAndToken(["voip"]);
2224
const user2 = await identityClient.createUserAndToken(["voip"]);
2325

26+
console.log("Creating room...");
27+
2428
// create RoomsClient
2529
const roomsClient = new RoomsClient(connectionString);
2630

@@ -42,7 +46,9 @@ async function main() {
4246
// create a room with the request payload
4347
const createRoom = await roomsClient.createRoom(createRoomOptions);
4448
const roomId = createRoom.id;
45-
console.log(`Created Room with ID ${roomId}`);
49+
console.log(`Successfully created room with id: ${roomId}.`);
50+
51+
console.log(`Adding new participant to room with id: ${roomId}...`);
4652

4753
// request payload to add participants
4854
const addParticipantsList = [
@@ -53,10 +59,17 @@ async function main() {
5359
];
5460

5561
// add user2 to the room with the request payload
56-
await roomsClient.addParticipants(roomId, addParticipantsList);
57-
const addParticipants = await roomsClient.getParticipants(roomId);
58-
console.log(`Added Participants`);
59-
printParticipants(addParticipants);
62+
await roomsClient.addOrUpdateParticipants(roomId, addParticipantsList);
63+
64+
console.log(`Successfully added participant to room with id: ${roomId}.`);
65+
console.log("Printing participants in room...");
66+
67+
let participantsInRoom = await roomsClient.listParticipants(roomId);
68+
await printParticipants(participantsInRoom);
69+
70+
await pause(500);
71+
72+
console.log("Updating role of participant...");
6073

6174
// request payload to update user1 with a new role
6275
const updateParticipantsList = [
@@ -67,35 +80,59 @@ async function main() {
6780
];
6881

6982
// update user1 with the request payload
70-
await roomsClient.updateParticipants(roomId, updateParticipantsList);
71-
console.log(`Updated Participants`);
72-
printParticipants(await roomsClient.getParticipants(roomId));
83+
await roomsClient.addOrUpdateParticipants(roomId, updateParticipantsList);
84+
console.log(`Successfully updated participant in room with id: ${roomId}.`);
85+
console.log("Printing updated participants in room...");
86+
87+
participantsInRoom = await roomsClient.listParticipants(roomId);
88+
await printParticipants(participantsInRoom);
89+
90+
await pause(500);
91+
92+
console.log("Removing participant from room...");
7393

7494
// request payload to delete both users from the room
7595
// this demonstrates both objects that can be used in deleting users from rooms: RoomParticipant or CommunicationIdentifier
7696
const removeParticipantsList = [user1.user, user2.user];
7797

7898
// remove both users from the room with the request payload
7999
await roomsClient.removeParticipants(roomId, removeParticipantsList);
80-
console.log(`Removed Participants`);
81-
printParticipants(await roomsClient.getParticipants(roomId));
100+
console.log(`Successfully removed participant from room with id: ${roomId}.`);
101+
102+
participantsInRoom = await roomsClient.listParticipants(roomId);
103+
await printParticipants(participantsInRoom);
104+
105+
await pause(500);
106+
107+
console.log(`Deleting room with id: ${roomId}...`);
82108

83109
// deletes the room for cleanup
84110
await roomsClient.deleteRoom(roomId);
111+
112+
console.log(`Successfully deleted room with id: ${roomId}.`);
85113
}
86114

87115
/**
88116
* Outputs the participants within a Participantsn to console.
89-
* @param pc - The Participants being printed to console.
117+
* @param participants - The Participants being printed to console.
90118
*/
91-
function printParticipants(participants) {
92-
console.log(`Number of Participants: ${participants.length}`);
93-
for (const participant of participants) {
94-
const id = getIdentifierRawId(participant.id);
95-
const role = participant.role;
96-
console.log(`${id} - ${role}`);
119+
async function printParticipants(participants) {
120+
var count = 0;
121+
for await (const participant of participants) {
122+
if (participant) {
123+
count++;
124+
const { role, id } = participant;
125+
console.log(`---Participant ${count}---`);
126+
console.log(`Kind: ${id?.kind}`);
127+
console.log(`Role: ${role}`);
128+
}
97129
}
98130
}
131+
132+
async function pause(time) {
133+
await new Promise((resolve) => setTimeout(resolve, time));
134+
}
135+
99136
main().catch((error) => {
100137
console.error("Encountered an error while sending request: ", error);
101138
process.exit(1);

0 commit comments

Comments
 (0)