Class: Client

Client

A Client is a starting point to access Twilio Programmable Chat functionality.

Methods


<static> create(token, options)

Factory method to create Chat client instance.
Parameters:
Name Type Description
token String Access token
options Client#ClientOptions 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

createChannel( [options])

Create a Channel on the server and subscribe to its events. Default options are public Channel type with empty uniqueName and friendlyName.
Parameters:
Name Type Argument Description
options Client#CreateChannelOptions <optional>
Options for the Channel
Returns:
Type
Promise.<Channel>

getChannelBySid(channelSid)

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

getChannelByUniqueName(uniqueName)

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

getLocalChannels( [sortingOptions])

Get array of Channels locally known to Client in provided sorting order. Locally known channels are the ones created and/or joined during client runtime and currently logged in User subscribed Channels. To ensure full list of subscribed Channels fetched - call the Client#getSubscribedChannels method and fetch all pages with help of Paginator#nextPage method.
Parameters:
Name Type Argument Description
sortingOptions Client#ChannelSortingOptions <optional>
Options for the Channel sorting
Returns:
Type
Promise.<Array.<Channel>>

getPublicChannelDescriptors()

Get the public channels directory content.
Returns:
Type
Promise.<Paginator.<ChannelDescriptor>>

getSubscribedChannels()

Get the current list of all subscribed Channels.
Returns:
Type
Promise.<Paginator.<Channel>>

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>

getUserChannelDescriptors()

Get the User's (created by, joined or invited to) channels directory content.
Returns:
Type
Promise.<Paginator.<ChannelDescriptor>>

getUserDescriptor(identity)

Gets user descriptor for given identity.
Parameters:
Name Type Description
identity String Identity of User
Returns:
User descriptor
Type
Promise.<UserDescriptor>

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
void | Error

setPushRegistrationId(channelType, registrationId)

Registers for push notifications.
Parameters:
Name Type Description
channelType Client#NotificationsChannelType 'gcm', 'apn' and 'fcm' are supported
registrationId string Push notification id provided by 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 'gcm', 'apn' and 'fcm' are supported
Returns:
Type
Promise.<void>

updateToken(token)

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

Type Definitions


ChannelSortingOptions

These options can be passed to Client#getLocalChannels.
Type:
  • Object
Properties:
Name Type Argument Description
criteria 'lastMessage' | 'friendlyName' | 'uniqueName' Sorting criteria for Channels array
order 'ascending' | 'descending' <optional>
Sorting order. If not present, then default is ascending

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'

CreateChannelOptions

These options can be passed to Client#createChannel.
Type:
  • Object
Properties:
Name Type Argument Description
attributes Object <optional>
Any custom attributes to attach to the Channel
friendlyName String <optional>
The non-unique display name of the Channel
isPrivate Boolean <optional>
Whether or not this Channel should be visible to uninvited Clients
uniqueName String <optional>
The unique identity name of the Channel

NotificationsChannelType

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

Events


channelAdded

Fired when a Channel becomes visible to the Client. Fired for created and not joined private channels and for all type of channels Client has joined or invited to.
Type: Channel

channelInvited

Fired when the Client is invited to a Channel.
Type: Channel

channelJoined

Fired when the Client joins a Channel.
Type: Channel

channelLeft

Fired when the Client leaves a Channel.
Type: Channel

channelRemoved

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

channelUpdated

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

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

memberJoined

Fired when a Member has joined the Channel.
Type: Member

memberLeft

Fired when a Member has left the Channel.
Type: Member

memberUpdated

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

messageAdded

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

messageRemoved

Fired when Message is removed from Channel'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

pushNotification

Fired when client received (and parsed) push notification via one of push channels (apn, gcm, 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 Member has stopped typing.
Type: Member

typingStarted

Fired when a Member has started typing.
Type: Member

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