Class: Client

Client

A Client is a starting point to access Twilio Conversations functionality.

Methods


<static> create(token [, options])

Factory method to create Conversations client instance.
Parameters:
Name Type Argument Description
token String Access token
options Client#ClientOptions <optional>
Options to customize the Client
Returns:
Type
Promise.<Client>

<static> parsePushNotification(notificationPayload)

Static method for push notification payload parsing. Returns parsed push as PushNotification object
Parameters:
Name Type Description
notificationPayload Object Push notification payload
Returns:
Type
PushNotification | Error

createConversation( [options])

Create a Conversation on the server and subscribe to its events. The default is a Conversation with an empty friendlyName.
Parameters:
Name Type Argument Description
options Client#CreateConversationOptions <optional>
Options for the Conversation
Returns:
Type
Promise.<Conversation>

getConversationBySid(conversationSid)

Get a known Conversation by its SID.
Parameters:
Name Type Description
conversationSid String Conversation sid
Returns:
Type
Promise.<Conversation>

getConversationByUniqueName(uniqueName)

Get a known Conversation by its unique identifier name.
Parameters:
Name Type Description
uniqueName String The unique identifier name of the Conversation to get
Returns:
Type
Promise.<Conversation>

getSubscribedConversations()

Get the current list of all subscribed Conversations.
Returns:
Type
Promise.<Paginator.<Conversation>>

getSubscribedUsers()

Returns:
List of subscribed User objects
Type
Promise.<Array.<User>>

getUser(identity)

Gets user for given identity, if it's in subscribed list - then return the user object from it, if not - then subscribes and adds user to the subscribed list.
Parameters:
Name Type Description
identity String Identity of User
Returns:
Fully initialized user
Type
Promise.<User>

handlePushNotification(notificationPayload)

Handle push notification payload parsing and emits event Client#event:pushNotification on this Client instance.
Parameters:
Name Type Description
notificationPayload Object Push notification payload
Returns:
Type
Promise.<void>

setPushRegistrationId(channelType, registrationId)

Registers for push notifications.
Parameters:
Name Type Description
channelType Client#NotificationsChannelType 'apn' and 'fcm' are supported
registrationId string Push notification id provided by the platform
Returns:
Type
Promise.<void>

shutdown()

Gracefully shutting down library instance.
Returns:
Type
Promise.<void>

unsetPushRegistrationId(channelType)

Unregisters from push notifications.
Parameters:
Name Type Description
channelType Client#NotificationsChannelType 'apn' and 'fcm' are supported
Returns:
Type
Promise.<void>

updateToken(token)

Update the token used by the Client and re-register with Conversations services.
Parameters:
Name Type Description
token String Access token
Returns:
Type
Promise.<Client>

Type Definitions


ClientOptions

These options can be passed to Client constructor.
Type:
  • Object
Properties:
Name Type Argument Default Description
logLevel String <optional>
'error' The level of logging to enable. Valid options (from strictest to broadest): ['silent', 'error', 'warn', 'info', 'debug', 'trace']

ConnectionState

Connection state of Client.
Type:
  • 'connecting' | 'connected' | 'disconnecting' | 'disconnected' | 'denied'

CreateConversationOptions

These options can be passed to Client#createConversation.
Type:
  • Object
Properties:
Name Type Argument Description
attributes any <optional>
Any custom attributes to attach to the Conversation
friendlyName String <optional>
The non-unique display name of the Conversation
uniqueName String <optional>
The unique identifier of the Conversation

NotificationsChannelType

Notifications channel type.
Type:
  • 'fcm' | 'apn'

Events


connectionError

Fired when connection is interrupted by unexpected reason
Type: Object
Properties:
Name Type Argument Description
terminal Boolean twilsock will stop connection attempts
message String root cause
httpStatusCode Number <optional>
http status code if available
errorCode Number <optional>
Twilio public error code if available

connectionStateChanged

Fired when Client's connection state has been changed.
Type: Client#ConnectionState

conversationAdded

Fired when a Conversation becomes visible to the Client. The event is also triggered when the client creates a new Conversation. Fired for all conversations Client has joined.
Type: Conversation

conversationJoined

Fired when the Client joins a Conversation.
Type: Conversation

conversationLeft

Fired when the Client leaves a Conversation.
Type: Conversation

conversationRemoved

Fired when a Conversation is no longer visible to the Client.
Type: Conversation

conversationUpdated

Fired when a Conversation's attributes or metadata have been updated. During Conversation's creation and initialization this event might be fired multiple times for same joined or created Conversation as new data is arriving from different sources.
Type: Object
Properties:
Name Type Description
conversation Conversation Updated Conversation
updateReasons Array.<Conversation#UpdateReason> Array of Conversation's updated event reasons

messageAdded

Fired when a new Message has been added to the Conversation on the server.
Type: Message

messageRemoved

Fired when Message is removed from Conversation's message list.
Type: Message

messageUpdated

Fired when an existing Message's fields are updated with new values.
Type: Object
Properties:
Name Type Description
message Message Updated Message
updateReasons Array.<Message#UpdateReason> Array of Message's updated event reasons

participantJoined

Fired when a Participant has joined the Conversation.
Type: Participant

participantLeft

Fired when a Participant has left the Conversation.
Type: Participant

participantUpdated

Fired when a Participant's fields has been updated.
Type: Object
Properties:
Name Type Description
participant Participant Updated Participant
updateReasons Array.<Participant#UpdateReason> Array of Participant's updated event reasons

pushNotification

Fired when client received (and parsed) push notification via one of push channels (apn or fcm).
Type: PushNotification

tokenAboutToExpire

Fired when token is about to expire and needs to be updated.
Type: void

tokenExpired

Fired when token is expired.
Type: void

typingEnded

Fired when a Participant has stopped typing.
Type: Participant

typingStarted

Fired when a Participant has started typing.
Type: Participant

userSubscribed

Fired when the Client is subscribed to a User.
Type: User

userUnsubscribed

Fired when the Client is unsubscribed from a User.
Type: User

userUpdated

Fired when the User's properties or reachability status have been updated.
Type: Object
Properties:
Name Type Description
user User Updated User
updateReasons Array.<User#UpdateReason> Array of User's updated event reasons