Skip to content

Commit

Permalink
[Communication][Rooms] throttle participant operations in samples to …
Browse files Browse the repository at this point in the history
…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)
  • Loading branch information
0rland0Wats0n authored and minhanh-phan committed Jun 12, 2023
1 parent c770b5f commit d0c2dbb
Show file tree
Hide file tree
Showing 10 changed files with 272 additions and 123 deletions.
5 changes: 3 additions & 2 deletions sdk/communication/communication-rooms/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ const roomsClient: RoomsClient = new RoomsClient(CONNECTION_STRING);

const validFrom = new Date(Date.now());
let validForDays = 10;
let validUntil = validFrom.setDate(validFrom.getTime() + validForDays);
let validUntil = new Date(validFrom.getTime());
validUntil.setDate(validFrom.getDate() + validForDays);

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

```js
validForDays = 60;
validUntil = validFrom.setDate(validFrom.getDate() + validForDays);
validUntil.setDate(validFrom.getDate() + validForDays);
const updateRoomOptions: UpdateRoomOptions = {
validFrom,
validUntil,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import { PagedAsyncIterableIterator } from "@azure/core-paging";
dotenv.config();

export async function main() {
console.log("Room Participant Operations JavaScript Sample");
console.log("_________________________________\n");

const connectionString =
process.env["COMMUNICATION_SAMPLES_CONNECTION_STRING"] ||
"endpoint=https://<resource-name>.communication.azure.com/;<access-key>";
Expand All @@ -27,6 +30,8 @@ export async function main() {
const user1 = await identityClient.createUserAndToken(["voip"]);
const user2 = await identityClient.createUserAndToken(["voip"]);

console.log("Creating room...");

// create RoomsClient
const roomsClient: RoomsClient = new RoomsClient(connectionString);

Expand All @@ -48,7 +53,9 @@ export async function main() {
// create a room with the request payload
const createRoom = await roomsClient.createRoom(createRoomOptions);
const roomId = createRoom.id;
console.log(`Created Room with ID ${roomId}`);
console.log(`Successfully created room with id: ${roomId}.`);

console.log(`Adding new participant to room with id: ${roomId}...`);

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

// add user2 to the room with the request payload
await roomsClient.addOrUpdateParticipants(roomId, addParticipantsList);
const addedParticipants = await roomsClient.listParticipants(roomId);
console.log(`Added Participants`);
printParticipants(addedParticipants);

console.log(`Successfully added participant to room with id: ${roomId}.`);
console.log("Printing participants in room...");

let participantsInRoom = await roomsClient.listParticipants(roomId);
await printParticipants(participantsInRoom);

await pause(500);

console.log("Updating role of participant...");

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

// update user1 with the request payload
await roomsClient.addOrUpdateParticipants(roomId, updateParticipantsList);
console.log(`Updated Participants`);
printParticipants(await roomsClient.listParticipants(roomId));
console.log(`Successfully updated participant in room with id: ${roomId}.`);
console.log("Printing updated participants in room...");

participantsInRoom = await roomsClient.listParticipants(roomId);
await printParticipants(participantsInRoom);

await pause(500);

console.log("Removing participant from room...");

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

// remove both users from the room with the request payload
await roomsClient.removeParticipants(roomId, removeParticipantsList);
console.log(`Removed Participants`);
printParticipants(await roomsClient.listParticipants(roomId));
console.log(`Successfully removed participant from room with id: ${roomId}.`);

participantsInRoom = await roomsClient.listParticipants(roomId);
await printParticipants(participantsInRoom);

await pause(500);

console.log(`Deleting room with id: ${roomId}...`);

// deletes the room for cleanup
await roomsClient.deleteRoom(roomId);

console.log(`Successfully deleted room with id: ${roomId}.`);
}

/**
* Outputs the participants within a Participantsn to console.
* @param pc - The Participants being printed to console.
* @param participants - The Participants being printed to console.
*/
async function printParticipants(
participants: PagedAsyncIterableIterator<Partial<RoomParticipant>>
): Promise<void> {
var count = 0;
for await (const participant of participants) {
const role = participant.role;
console.log(role);
if (participant) {
count++;
const { role, id } = participant;
console.log(`---Participant ${count}---`);
console.log(`Kind: ${id?.kind}`);
console.log(`Role: ${role}`);
}
}
}

async function pause(time: number): Promise<void> {
await new Promise((resolve) => setTimeout(resolve, time));
}

main().catch((error) => {
console.error("Encountered an error while sending request: ", error);
process.exit(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,24 @@ import * as dotenv from "dotenv";
dotenv.config();

export async function main() {
console.log("Room Operations JavaScript Sample");
console.log("_________________________________\n\n");

const connectionString =
process.env["COMMUNICATION_SAMPLES_CONNECTION_STRING"] ||
"endpoint=https://<resource-name>.communication.azure.com/;<access-key>";

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

console.log("Creating room...");

// create RoomsClient
const roomsClient: RoomsClient = new RoomsClient(connectionString);

var validFrom = new Date(Date.now());
var validUntil = new Date(validFrom.getTime() + 5 * 60 * 1000);
var validForDays = 10;
var validUntil = addDays(validFrom, validForDays);

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

console.log(`Fetching room with id: ${roomId}...`);

// retrieves the room with corresponding ID
const getRoom = await roomsClient.getRoom(roomId);
console.log(`Retrieved Room with ID ${roomId}`);
console.log(`Successfully fetched room with id: ${roomId}. Room details:`);
printRoom(getRoom);

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

// request payload to update a room
const updateRoomOptions: UpdateRoomOptions = {
validFrom,
validUntil,
validUntil: addDays(validUntil, 10),
};

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

console.log(`Deleting room with id: ${roomId}...`);

// deletes the specified room
await roomsClient.deleteRoom(roomId);

console.log(`Successfully deleted room with id: ${roomId}.`);
}

/**
Expand All @@ -79,7 +90,18 @@ export async function main() {
function printRoom(room: CommunicationRoom): void {
console.log(`Room ID: ${room.id}`);
console.log(`Valid From: ${room.validFrom}`);
console.log(`Valid Until: ${room.validUntil}`);
console.log(`Valid Until: ${room.validUntil}\n\n`);
}

/**
* Adds the specified ampunt of days
* @param current - The original date
* @param days - The number of days to add
*/
function addDays(current: Date, days: number): Date {
const copied = new Date(current.getTime());
copied.setDate(copied.getDate() + days);
return copied;
}

main().catch((error) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@
"@azure/communication-rooms": "next",
"dotenv": "latest",
"@azure/communication-identity": "^1.1.0-beta.2",
"@azure/communication-common": "^2.1.0"
"@azure/core-paging": "^1.5.0"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@

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

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

async function main() {
console.log("Room Participant Operations JavaScript Sample");
console.log("_________________________________\n");

const connectionString =
process.env["COMMUNICATION_SAMPLES_CONNECTION_STRING"] ||
"endpoint=https://<resource-name>.communication.azure.com/;<access-key>";
Expand All @@ -21,6 +23,8 @@ async function main() {
const user1 = await identityClient.createUserAndToken(["voip"]);
const user2 = await identityClient.createUserAndToken(["voip"]);

console.log("Creating room...");

// create RoomsClient
const roomsClient = new RoomsClient(connectionString);

Expand All @@ -42,7 +46,9 @@ async function main() {
// create a room with the request payload
const createRoom = await roomsClient.createRoom(createRoomOptions);
const roomId = createRoom.id;
console.log(`Created Room with ID ${roomId}`);
console.log(`Successfully created room with id: ${roomId}.`);

console.log(`Adding new participant to room with id: ${roomId}...`);

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

// add user2 to the room with the request payload
await roomsClient.addParticipants(roomId, addParticipantsList);
const addParticipants = await roomsClient.getParticipants(roomId);
console.log(`Added Participants`);
printParticipants(addParticipants);
await roomsClient.addOrUpdateParticipants(roomId, addParticipantsList);

console.log(`Successfully added participant to room with id: ${roomId}.`);
console.log("Printing participants in room...");

let participantsInRoom = await roomsClient.listParticipants(roomId);
await printParticipants(participantsInRoom);

await pause(500);

console.log("Updating role of participant...");

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

// update user1 with the request payload
await roomsClient.updateParticipants(roomId, updateParticipantsList);
console.log(`Updated Participants`);
printParticipants(await roomsClient.getParticipants(roomId));
await roomsClient.addOrUpdateParticipants(roomId, updateParticipantsList);
console.log(`Successfully updated participant in room with id: ${roomId}.`);
console.log("Printing updated participants in room...");

participantsInRoom = await roomsClient.listParticipants(roomId);
await printParticipants(participantsInRoom);

await pause(500);

console.log("Removing participant from room...");

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

// remove both users from the room with the request payload
await roomsClient.removeParticipants(roomId, removeParticipantsList);
console.log(`Removed Participants`);
printParticipants(await roomsClient.getParticipants(roomId));
console.log(`Successfully removed participant from room with id: ${roomId}.`);

participantsInRoom = await roomsClient.listParticipants(roomId);
await printParticipants(participantsInRoom);

await pause(500);

console.log(`Deleting room with id: ${roomId}...`);

// deletes the room for cleanup
await roomsClient.deleteRoom(roomId);

console.log(`Successfully deleted room with id: ${roomId}.`);
}

/**
* Outputs the participants within a Participantsn to console.
* @param pc - The Participants being printed to console.
* @param participants - The Participants being printed to console.
*/
function printParticipants(participants) {
console.log(`Number of Participants: ${participants.length}`);
for (const participant of participants) {
const id = getIdentifierRawId(participant.id);
const role = participant.role;
console.log(`${id} - ${role}`);
async function printParticipants(participants) {
var count = 0;
for await (const participant of participants) {
if (participant) {
count++;
const { role, id } = participant;
console.log(`---Participant ${count}---`);
console.log(`Kind: ${id?.kind}`);
console.log(`Role: ${role}`);
}
}
}

async function pause(time) {
await new Promise((resolve) => setTimeout(resolve, time));
}

main().catch((error) => {
console.error("Encountered an error while sending request: ", error);
process.exit(1);
Expand Down
Loading

0 comments on commit d0c2dbb

Please sign in to comment.