Class: Application

Application


new Application(SDK, params)

Core application class for the SDK.
Application is the parent object holding the list of conversations, the session object.
Provides methods to create conversations and retrieve a list of the user's conversations, while it holds the listeners for
user's invitations

Parameters:
Name Type Description
SDK NexmoClient

session Object

params object
Source:
Fires:
Example

Accessing the list of conversations

rtc.login(token).then((application) => {
   console.log(application.conversations);
   console.log(application.me.name, application.me.id);
 });

Methods


<async> callServer(user [, type])

Creates a call to phone a number.
The call object is created under application.calls when the call has started.
listen for it with application.on("call:status:changed")

You don't need to start the stream, the SDK will play the audio for you

Parameters:
Name Type Argument Default Description
user string

the phone number or the username you want to call

type string <optional>
"phone"

the type of the call you want to have. possible values "phone" or "app" (default is "phone")

Source:
Returns:
Type
Promise.<NXMCall>
Example

Create a call to a phone

application.on("call:status:changed", (nxmCall) => {
   if (nxmCall.status === nxmCall.CALL_STATUS.STARTED) {
		  console.log('the call has started');
		}
 });

 application.callServer(phone_number).then(() => {
   console.log('Calling phone ' + phone_number);
 });

<async> getConversation(id)

Query the service to see if this conversation exists with the
logged in user as a member and retrieve the data object
Result added (or updated) in this.conversations

Parameters:
Name Type Description
id string

the id of the conversation to fetch

Source:
Returns:
  • the requested conversation
Type
Promise.<Conversation>

<async> getConversations(params)

Query the service to obtain a complete list of conversations of which the
logged-in user is a member with a state of JOINED or INVITED.

Parameters:
Name Type Description
params object

configure defaults for paginated conversations query

Properties
Name Type Argument Description
order string

'asc' or 'desc' ordering of resources based on creation time

page_size number

the number of resources returned in a single request list

cursor string <optional>

string to access the starting point of a dataset

Source:
Returns:
  • Populate Application.conversations.
Type
Promise.<Page.<Map.<Conversation>>>
Example

Get Conversations

application.getConversations({ page_size: 20 ).then((conversations_page) => {
   conversations_page.items.forEach(conversation => {
     render(conversation)
   })
 });

<async> getUser( [id])

Get Details of a user

Parameters:
Name Type Argument Description
id string <optional>

the id of the user to fetch, if skipped, it returns your own user details

Source:
Returns:
Type
Promise.<User>

<async> inAppCall(usernames)

Creates a call to specified user/s.

Parameters:
Name Type Description
usernames Array.<string>

the user names for those we want to call

Source:
Returns:

a NXMCall object with all the call properties

Type
Promise.<NXMCall>

<async> newConversation( [params])

Query the service to create a new conversation
The conversation name must be unique per application.

Parameters:
Name Type Argument Description
params object <optional>

leave empty to get a GUID as name

Properties
Name Type Description
name string

the name of the conversation. A UID will be assigned if this is skipped

display_name string

the display_name of the conversation.

Source:
Returns:
  • the created Conversation
Type
Promise.<Conversation>
Example

Create a conversation and join

application.newConversation().then((conversation) => {
   //join the created conversation
   conversation.join().then((member) => {
     //Get the user's member belonging in this conversation.
     //You can also access it via conversation.me
     console.log("Joined as " + member.user.name);
 }).catch((error) => {
   console.log(error);
 });

newConversationAndJoin( [params])

Query the service to create a new conversation and join it
The conversation name must be unique per application.

Parameters:
Name Type Argument Description
params object <optional>

leave empty to get a GUID as name

Properties
Name Type Description
name string

the name of the conversation. A UID will be assigned if this is skipped

display_name string

the display_name of the conversation.

Source:
Returns:
  • the created Conversation
Type
Promise.<Conversation>
Example

Create a conversation and join

application.newConversationAndJoin().then((conversation) => {
   //join the created conversation
   conversation.join().then((member) => {
     //Get the user's member belonging in this conversation.
     //You can also access it via conversation.me
     console.log("Joined as " + member.user.name);
 }).catch((error) => {
   console.log(error);
 });

syncConversations()

Fetching all the conversations and sync progress events

Source:

updateToken(token)

update user's token

Parameters:
Name Type Description
token string

the new token

Source:
Returns:
Type
Promise

Events


call:status:changed

NXMCall listening for nxmCall status changed events.

Properties:
Name Type Description
nxmCall NXMCall

the actual event

Source:
Example

listen for nxmCall status events

application.on("call:status:changed",(nxmCall) => {
   console.log("call: " + nxmCall.status);
 });

NXM-errors

Application listening for joins.

Properties:
Name Type Description
error NexmoClientError
Source:
Examples

listen for errors

application.on('*', 'NXM-errors', (error) => {
   console.log('Error thrown with type ' + error.type);
 });

Update the token on expired-token error

application.on('system:error:expired-token', 'NXM-errors', (error) => {
	console.log('token expired');
	application.updateToken(<token>);
});

member:call

Application listening for calls.

Properties:
Name Type Description
member Member

the member that initiated the nxmCall

nxmCall NXMCall

resolves the nxmCall object

Source:
Example

listen for calls in Application level

application.on("member:call", (member, nxmCall) => {
   console.log("NXMCall ", nxmCall);
 });

member:invited

Application listening for invites.

Properties:
Name Type Description
member Member

The invited member

event NXMEvent

The invitation event

Source:
Example

listen for your invites

application.on("member:invited",(member, event) => {
   console.log("Invited to the conversation: " + event.conversation.display_name || event.conversation.name);
   // identify the sender.
   console.log("Invited by: " + member.invited_by);
   //accept an invitation.
   application.conversations.get(event.conversation.id).join();
   //decline the invitation.
    application.conversations.get(event.conversation.id).leave();

member:joined

Application listening for joins.

Properties:
Name Type Description
member Member

the member that joined the conversation

event NXMEvent

the join event

Source:
Example

listen join events in Application level

application.on("member:joined",(member, event) => {
   console.log("JOINED", "Joined conversation: " + event.conversation.display_name || event.conversation.name);
 });

rtcstats:report

Application listening for RTC stats.

Properties:
Name Type Description
MOS number

the calculated MOS score

report Object

the stats report from WebRTC | when the call has ended this is null, see the mos_report for final MOS summary

Conversation Conversation

the conversation the report belongs to

mos_report Object

a report for the MOS values

Properties
Name Type Description
min string

the minimum MOS value during the stream

max string

the maximum MOS value during the stream

last string

the last MOS value during the stream

average string

the average MOS value during the stream

Source:
Example

listening for quality mos score

application.on("rtcstats:report",(mos, report, conversation, mos_report) => {
   console.log("call quality (MOS)", mos);
   if (mos_report) {
     console.log('mos_report', mos_report);
   }
 });

sync:progress

Application listening sync status.

Properties:
Name Type Description
status.sync_progress number

Percentage of fetched conversations

Source:
Example

listening for changes in the synchronisation progress

application.on("sync:progress",(status) => {
	  console.log(data.sync_progress);
 });