Managing Members with the Client SDK

This guide covers how to manage Members with the Vonage Client SDK. Before you begin, make sure you added the SDK to your app and created a Session (Android, iOS, JS).

When a User joins a Conversation they become a Member. A membership is a mapping between a User, a Channel, and a Conversation. For example, a User called "Alice", will become a Member in a Conversation named "Chat", via the "App" channel.

Member actions such inviting and listing Members are possible by any User with a Conversation ID. To control which Users can perform these actions, restrict via ACLs on the JWT.

Joining a Conversation

Given a Conversation ID, you are able to join a Conversation. Joining creates a Member in the Conversation with the JOINED state. After this, you can send and receive messages. When joining a Conversation via the Client SDK, the channel is set to "App"

client.joinConversation(conversationId)
.then(memberId => {
    console.log("Successfully joined Conversation with Member Id: ", memberId);
}).catch(error => {
    console.error("Error joining Conversation: ", error);
});

Inviting to a Conversation

Given a Conversation ID and the username of another User, you can invite them to a Conversation. On Android and iOS, a push notification will be sent to the registered devices of the invited User.

client.inviteToConversation(conversationId, username)
    .then(memberId => {
        console.log("Successfully invited User to Conversation with Member Id: ", memberId);
    }).catch(error => {
        console.error("Error joining Conversation: ", error);
    });

Leaving a Conversation

Given a Conversation ID, you are able to leave a Conversation. Leaving a Conversation updates the existing Member's state to LEFT.

client.leaveConversation(conversationId)
    .then(() => {
        console.log("Successfully left Conversation.");
    }).catch(error => {
        console.error("Error leaving Conversation: ", error);
    });

Getting Conversation Members

Given a Conversation ID of a Conversation where you are a Member, you can get all Members of that Conversation. You can optionally pass in some parameters to configure the response, if not the default values will be used. This method returns a paginated response. If you are unfamiliar with pagination, view the pagination guide.

You can set:

  • Order
  • Page Size
  • A cursor
const params = {
    order: "asc", // "desc"
    pageSize: 100,
    cursor: null
};
    
client.getConversationMembers(conversationId, params)
    .then(({members, nextCursor, previousCursor}) => {
        console.log("Array of Members: ", members);
        console.log("cursor for next set of results, if any. could be null: ", nextCursor);
        console.log("cursor for previous set of results, if any. could be null: ", previousCursor);
    }).catch(error => {
        console.error("Error getting Members: ", error);
    });

Getting A Conversation Member

Given a Conversation ID and Member ID, you can get a full Member object. If you would like to get your Member object, you can set the memberId parameter to "me".

client.getConversationMember(conversationId, memberId)
    .then(member => {
        console.log("Successfully got Member: ", member);
    }).catch(error => {
        console.error("Error getting Member: ", error);
    });

Getting A Conversation Member Events

You can receive all Conversation Member Events in your application by setting up an Event Listener/Delegate Function. Here you can check the kind of incoming Member Event.

client.on("conversationEvent", event => {
    switch (event.kind) {
        case "member:invited":
            handleMemberInvited(event);
            break;
        case "member:joined":
            handleMemberJoined(event);
            break;
        case "member:left":
            handleMemberLeft(event);
            break;
    };
});