Quickstart: Create and manage a room resource

Important

This feature of Azure Communication Services is currently in public preview. Preview APIs and SDKs are provided without a service-level agreement, and are not recommended for production workloads. Certain features might not be supported or might have constrained capabilities. For more information, see Supplemental Terms of Use for Microsoft Azure Previews.

This quickstart will help you get started with Azure Communication Services Rooms. A room is a server-managed communications space for a known, fixed set of participants to collaborate for a pre-determined duration. The rooms conceptual documentation covers more details and use cases for rooms.

Prerequisites

Sample code

You can review and download the sample code for this quick start on GitHub.

Setting up

Create a new C# application

In a console window (such as cmd, PowerShell, or Bash), use the dotnet new command to create a new console app with the name RoomsQuickstart. This command creates a simple "Hello World" C# project with a single source file: Program.cs.

dotnet new console -o RoomsQuickstart

Change your directory to the newly created app folder and use the dotnet build command to compile your application.

cd RoomsQuickstart
dotnet build

Install the package

Install the Azure Communication Rooms client library for .NET with [NuGet][https://www.nuget.org/]:

dotnet add package Azure.Communication.Rooms

You'll need to use the Azure Communication Rooms client library for .NET version 1.0.0-beta.1 or above.

Initialize a room client

Create a new RoomsClient object that will be used to create new rooms and manage their properties and lifecycle. The connection string of your Communications Service will be used to authenticate the request. For more information on connection strings, see this page.

// Find your Communication Services resource in the Azure portal
var connectionString = "<connection_string>";
RoomsClient roomsClient = new RoomsClient(connectionString);

Create a room

Create a new room with default properties using the code snippet below:

CommunicationRoom createdRoom = await RoomCollection.CreateRoomAsync(validFrom, validUntil, RoomJoinPolicy.InviteOnly, roomParticipants, cancellationToken);
string roomId = createdRoom.Id;

Since rooms are server-side entities, you may want to keep track of and persist the roomId in the storage medium of choice. You can reference the roomId to view or update the properties of a room object.

Get properties of an existing room

Retrieve the details of an existing room by referencing the roomId:

CommunicationRoom getRoomResponse = await roomsClient.GetRoomAsync(roomId);

Update the lifetime of a room

The lifetime of a room can be modified by issuing an update request for the ValidFrom and ValidUntil parameters. A room can be valid for a maximum of six months.

DateTimeOffset  validFrom = new DateTime(2022, 05, 01, 00, 00, 00, DateTimeKind.Utc);
DateTimeOffset  validUntil = validFrom.AddDays(1);

CommunicationRoom updatedRoom = await RoomCollection.UpdateRoomAsync(roomId, validFrom, validUntil, RoomJoinPolicy.InviteOnly, null, cancellationToken);

Add new participants

To add new participants to a room, use the AddParticipantsAsync method exposed on the client.

var communicationUser1 = "<CommunicationUserId1>";
var communicationUser2 = "<CommunicationUserId2>";
var communicationUser3 = "<CommunicationUserId3>";

List<RoomParticipant> participants = new List<RoomParticipant>();
participants.Add(new RoomParticipant(new CommunicationUserIdentifier(communicationUser1), "Presenter"));
participants.Add(new RoomParticipant(new CommunicationUserIdentifier(communicationUser2), "Attendee"));
participants.Add(new RoomParticipant(new CommunicationUserIdentifier(communicationUser3), "Attendee"));

var addParticipantsResult = await RoomCollection.AddParticipantsAsync(roomId, participants);

Participants that have been added to a room become eligible to join calls.

Get list of participants

Retrieve the list of participants for an existing room by referencing the roomId:

ParticipantsCollection existingParticipants = await RoomCollection.GetParticipantsAsync(roomId);

Remove participants

To remove a participant from a room and revoke their access, use the RemoveParticipantsAsync method.

var communicationUser = "<CommunicationUserId1>";

List<CommunicationIdentifier> participants = new List<CommunicationIdentifier>();
participants.Add(new CommunicationUserIdentifier(communicationUser));

var removeParticipantsResult = await RoomCollection.RemoveParticipantsAsync(roomId, participants);

Delete room

If you wish to disband an existing room, you may issue an explicit delete request. All rooms and their associated resources are automatically deleted at the end of their validity plus a grace period.

Response deleteRoomResponse = await RoomCollection.DeleteRoomAsync(createdRoomId);

Reference documentation

Read about the full set of capabilities of Azure Communication Services rooms from the .NET SDK reference or REST API reference.

Prerequisites

Sample code

You can review and download the sample code for this quick start on GitHub.

Setting up

Create a new Java application

In a console window (such as cmd, PowerShell, or Bash), use the mvn command below to create a new console app with the name rooms-quickstart. This command creates a simple "Hello World" Java project with a single source file: App.java.

mvn archetype:generate -DgroupId=com.contoso.app -DartifactId=rooms-quickstart -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false

Include the package

You'll need to use the Azure Communication Rooms client library for Java version 1.0.0-beta.2 or above.

Include the BOM file

Include the azure-sdk-bom to your project to take dependency on the General Availability (GA) version of the library. In the following snippet, replace the {bom_version_to_target} placeholder with the version number. To learn more about the BOM, see the Azure SDK BOM readme.

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.azure</groupId>
            <artifactId>azure-sdk-bom</artifactId>
            <version>{bom_version_to_target}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

and then include the direct dependency in the dependencies section without the version tag.

<dependencies>
  <dependency>
    <groupId>com.azure</groupId>
    <artifactId>azure-communication-rooms</artifactId>
  </dependency>
</dependencies>

Include direct dependency

If you want to take dependency on a particular version of the library that isn't present in the BOM, add the direct dependency to your project as follows.

<dependency>
  <groupId>com.azure</groupId>
  <artifactId>azure-communication-rooms</artifactId>
  <version>1.0.0-beta.1</version>
</dependency>

Initialize a room client

Create a new RoomsClient object that will be used to create new rooms and manage their properties and lifecycle. The connection string of your Communications Service will be used to authenticate the request. For more information on connection strings, see this page.

// Find your Communication Services resource in the Azure portal
String connectionString = "<connection string>";
RoomsClient roomsClient = new RoomsClientBuilder().connectionString(connectionString).buildClient();

// Set communication user id
static String USER_ID_1 = "<communication-user-id-1>";
static String USER_ID_2 = "<communication-user-id-2>";
static String USER_ID_3 = "<communication-user-id-3>";

Create a room

Create a new room with default properties using the code snippet below:

OffsetDateTime validFrom = OffsetDateTime.now();
OffsetDateTime validUntil = validFrom.plusDays(30);
RoomJoinPolicy roomJoinPolicy = RoomJoinPolicy.INVITE_ONLY;

List<RoomParticipant> roomParticipants = new ArrayList<RoomParticipant>();

roomParticipants.add(new RoomParticipant().setCommunicationIdentifier(new CommunicationUserIdentifier(USER_ID_1)).setRole(RoleType.CONSUMER));
roomParticipants.add(new RoomParticipant().setCommunicationIdentifier(new CommunicationUserIdentifier(USER_ID_2)).setRole(RoleType.ATTENDEE));

return roomsClient.createRoom(
    validFrom,
    validUntil,
    roomJoinPolicy,
    roomParticipants
);

Since rooms are server-side entities, you may want to keep track of and persist the roomId in the storage medium of choice. You can reference the roomId to view or update the properties of a room object.

Get properties of an existing room

Retrieve the details of an existing room by referencing the roomId:

CommunicationRoom roomResult = roomsClient.getRoom(roomId);

Update the lifetime of a room

The lifetime of a room can be modified by issuing an update request for the ValidFrom and ValidUntil parameters. A room can be valid for a maximum of six months.

OffsetDateTime validFrom = OffsetDateTime.now().plusDays(1);
OffsetDateTime validUntil = validFrom.plusDays(1);

CommunicationRoom roomResult = roomsClient.updateRoom(roomId, validFrom, validUntil);

Add new participants

To add new participants to a room, use the addParticipants method exposed on the client.

RoomParticipant newParticipant = new RoomParticipant().setCommunicationIdentifier(new CommunicationUserIdentifier(USER_ID_3)).setRole(RoleType.CONSUMER);
ParticipantsCollection updatedParticipants = roomsClient.addParticipants(roomId, List.of(newParticipant));

Participants that have been added to a room become eligible to join calls.

Get list of participants

Retrieve the list of participants for an existing room by referencing the roomId:

try {
     ParticipantsCollection participants = roomsClient.getParticipants(roomId);
     System.out.println("Participants: \n" + listParticipantsAsString(participants.getParticipants()));
} catch (Exception ex) {
    System.out.println(ex);
}

Remove participants

To remove a participant from a room and revoke their access, use the removeParticipants method.

RoomParticipant existingParticipant = new RoomParticipant().setCommunicationIdentifier(new CommunicationUserIdentifier(USER_ID_1));
ParticipantsCollection updatedParticipants = roomsClient.removeParticipants(roomId, List.of(existingParticipant));

Delete room

If you wish to disband an existing room, you may issue an explicit delete request. All rooms and their associated resources are automatically deleted at the end of their validity plus a grace period.

roomsClient.deleteRoomWithResponse(roomId, Context.NONE);

Reference documentation

Read about the full set of capabilities of Azure Communication Services rooms from the Java SDK reference or REST API reference.

Prerequisites

Sample code

You can review and download the sample code for this quick start on GitHub.

Setting up

Create a new python application

In a terminal or console window, create a new folder for your application and navigate to it.

mkdir acs-rooms-quickstart && cd acs-rooms-quickstart

Create a new file called rooms-quickstart.py and add the basic program structure.

from azure.communication.rooms import (
    RoomsClient,
    RoomParticipant,
    RoleType,
    RoomJoinPolicy
)
from azure.communication.identity import CommunicationUserIdentifier

class RoomsQuickstart(object):
    #room method implementations go here

if __name__ == '__main__':
    rooms = RoomsQuickstart()

Install the packages

You'll need to use the Azure Communication Rooms client library for Python version 1.0.0b2 or above.

From a console prompt, navigate to the directory containing the rooms.py file, then execute the following command:

pip install azure-communication-rooms
pip install azure-communication-identity

Initialize a room client

Create a new RoomsClient object that will be used to create new rooms and manage their properties and lifecycle. The connection string of your Communications Service will be used to authenticate the request. For more information on connection strings, see this page.

#Find your Communication Services resource in the Azure portal
self.connection_string = <COMMUNICATION_SAMPLES_CONNECTION_STRING>
self.rooms_client = RoomsClient.from_connection_string(self.connection_string)

Create a room

Create a new room with default properties using the code snippet below:

valid_from = datetime.now()
valid_until = valid_from + relativedelta(months=+1)
participants = []
participants.append(RoomParticipant(CommunicationUserIdentifier("<ACS User MRI identity 1>")))
participants.append(RoomParticipant(CommunicationUserIdentifier("<ACS User MRI identity 2>")))
participants.append(RoomParticipant(CommunicationUserIdentifier("<ACS User MRI identity 3>")))

try:
    create_room_response = self.rooms_client.create_room(valid_from=valid_from, valid_until=valid_until, participants=participants)
except HttpResponseError as ex:
    print(ex)

Since rooms are server-side entities, you may want to keep track of and persist the roomId in the storage medium of choice. You can reference the roomId to view or update the properties of a room object.

Get properties of an existing room

Retrieve the details of an existing room by referencing the roomId:

try:
    get_room_response = self.rooms_client.get_room(room_id=room_id)
except HttpResponseError as ex:
    print(ex)

Update the lifetime of a room

The lifetime of a room can be modified by issuing an update request for the ValidFrom and ValidUntil parameters. A room can be valid for a maximum of six months.

# set attributes you want to change
valid_from =  datetime.now()
valid_until = valid_from + relativedelta(months=+1,days=+20)

try:
    update_room_response = self.rooms_client.update_room(room_id=room_id, valid_from=valid_from, valid_until=valid_until)
    self.printRoom(response=update_room_response)
except HttpResponseError as ex:
    print(ex)

Add new participants

To add new participants to a room, use the add_participants method exposed on the client.

try:
    participants = []
    participants.append(RoomParticipant(CommunicationUserIdentifier("<ACS User MRI identity 1>"), RoleType.ATTENDEE))
    self.rooms_client.add_participants(room_id, participants)

except Exception as ex:
    print('Error in adding participants to room.', ex)

Participants that have been added to a room become eligible to join calls.

Get list of participants

Retrieve the list of participants for an existing room by referencing the roomId:

try:
    participants = self.rooms_client.get_participants(room_id)
except HttpResponseError as ex:
    print(ex)

Remove participants

To remove a participant from a room and revoke their access, use the remove_participants method.

participants = [CommunicationUserIdentifier("<ACS User MRI identity 2>")]

try:
    remove_participants_response = self.rooms_client.remove_participants(room_id=room_id, communication_identifiers=participants)
except HttpResponseError as ex:
    print(ex)

Delete room

If you wish to disband an existing room, you may issue an explicit delete request. All rooms and their associated resources are automatically deleted at the end of their validity plus a grace period.

self.rooms_client.delete_room(room_id=room)

Reference documentation

Read about the full set of capabilities of Azure Communication Services rooms from the Python SDK reference or REST API reference.

Prerequisites

Sample code

You can review and download the sample code for this quick start on GitHub.

Setting up

Create a new web application

In a terminal or console window, create a new folder for your application and navigate to it.

mkdir acs-rooms-quickstart && cd acs-rooms-quickstart

Run npm init to create a package.json file with default settings.

npm init -y

Install the packages

You'll need to use the Azure Communication Rooms client library for JavaScript version 1.0.0-beta.1 or above.

Use the npm install command to install the below Communication Services SDKs for JavaScript.

npm install @azure/communication-rooms --save
npm install @azure/communication-identity --save

Initialize a room client

Create a new RoomsClient object that will be used to create new rooms and manage their properties and lifecycle. The connection string of your Communications Service will be used to authenticate the request. For more information on connection strings, see this page.

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

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

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

Create a room

Create a new room with default properties using the code snippet below:

// options payload to create a room
const createRoomOptions = {
  validFrom: validFrom,
  validUntil: validUntil,
  roomJoinPolicy: "InviteOnly",
  participants: [
    {
      id: user1.user,
      role: "Attendee",
    },
  ]
};
  
// create a room with the request payload
const createRoom = await roomsClient.createRoom(createRoomOptions);
const roomId = createRoom.id;

Since rooms are server-side entities, you may want to keep track of and persist the roomId in the storage medium of choice. You can reference the roomId to view or update the properties of a room object.

Get properties of an existing room

Retrieve the details of an existing room by referencing the roomId:

// retrieves the room with corresponding ID
const getRoom = await roomsClient.getRoom(roomId);

Update the lifetime of a room

The lifetime of a room can be modified by issuing an update request for the ValidFrom and ValidUntil parameters. A room can be valid for a maximum of six months.

validFrom.setTime(validUntil.getTime());
validUntil.setTime(validFrom.getTime() + 5 * 60 * 1000);

// request payload to update a room
const updateRoomOptions = {
  validFrom: validFrom,
  validUntil: validUntil
};

// updates the specified room with the request payload
const updateRoom = await roomsClient.updateRoom(roomId, updateRoomOptions);

Add new participants

To add new participants to a room, use the addParticipants method exposed on the client.

  // request payload to add participants
  const addParticipantsList = {
    participants: [
      {
        id: user2.user,
        role: "Consumer",
      },
    ],
  };

  // add user2 to the room with the request payload
  const addParticipants = await roomsClient.addParticipants(roomId, addParticipantsList);

Participants that have been added to a room become eligible to join calls.

Get list of participants

Retrieve the list of participants for an existing room by referencing the roomId:

  const participantsList = await roomsClient.getParticipants(roomId);

Remove participants

To remove a participant from a room and revoke their access, use the removeParticipants method.

  // request payload to delete both users from the room
  const removeParticipantsList = {
    participants: [user1.user, user2.user],
  };

  // remove both users from the room with the request payload
  await roomsClient.removeParticipants(roomId, removeParticipantsList);

Delete room

If you wish to disband an existing room, you may issue an explicit delete request. All rooms and their associated resources are automatically deleted at the end of their validity plus a grace period.

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

Reference documentation

Read about the full set of capabilities of Azure Communication Services rooms from the JavaScript SDK reference or REST API reference.

Object model

The table below lists the main properties of room objects:

Name Description
roomId Unique room identifier.
validFrom Earliest time a room can be used.
validUntil Latest time a room can be used.
roomJoinPolicy Specifies which user identities are allowed to join room calls. Valid options are InviteOnly and CommunicationServiceUsers.
participants List of participants to a room. Specified as a CommunicationIdentifier.
roleType The role of a room participant. Can be either Presenter, Attendee, or Consumer.

Next steps

Once you've created the room and configured it, you can learn how to join a rooms call.

In this section you learned how to:

  • Create a new room
  • Get the properties of a room
  • Update the properties of a room
  • Delete a room

You may also want to: