Options
All
  • Public
  • Public/Protected
  • All
Menu

A client is the starting point to the Twilio Programmable Chat functionality.

Hierarchy

  • ReplayEventEmitter<ClientEvents>
    • Client

Index

Constructors

constructor

  • Returned client instance is not yet fully initialized. Calling any operations will block until it is. Use connection events to monitor when client becomes fully available (connectionStateChanged with state 'connected') or not available (connectionStateChange with state 'denied', event tokenExpired, event connectionError).

    Parameters

    • fpaToken: string

      Access token

    • options: ClientOptions = {}

      Options to customize the Client

    Returns Client

    A not yet fully-initialized client.

Events

Static Readonly channelAdded

channelAdded: "channelAdded" = 'channelAdded'

Fired when a channel becomes visible to the client. The event is also triggered when the client creates a new channel. Fired for all channels client has joined.

Parameters:

  1. Channel channel - the channel in question

Static Readonly channelInvited

channelInvited: "channelInvited" = 'channelInvited'

Fired when the client gets invited to a channel.

Parameters:

  1. Channel channel - the channel in question

Static Readonly channelJoined

channelJoined: "channelJoined" = 'channelJoined'

Fired when the client joins a channel.

Parameters:

  1. Channel channel - the channel in question

Static Readonly channelLeft

channelLeft: "channelLeft" = 'channelLeft'

Fired when the client leaves a channel.

Parameters:

  1. Channel channel - the channel in question

Static Readonly channelRemoved

channelRemoved: "channelRemoved" = 'channelRemoved'

Fired when a channel is no longer visible to the client.

Parameters:

  1. Channel channel - the channel in question

Static Readonly channelUpdated

channelUpdated: "channelUpdated" = 'channelUpdated'

Fired when the attributes or the metadata of a channel 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.

Parameters:

  1. object data - info object provided with the event. It has the following properties:

Static Readonly connectionError

connectionError: "connectionError" = 'connectionError'

Fired when the connection is interrupted for an unexpected reason.

Parameters:

  1. object data - info object provided with the event. It has the following properties:
    • boolean terminal - Twilsock will stop connection attempts if true
    • string message - the error message of the root cause
    • number? httpStatusCode - http status code if available
    • number? errorCode - Twilio public error code if available

Static Readonly connectionStateChanged

connectionStateChanged: "connectionStateChanged" = 'connectionStateChanged'

Fired when the connection state of the client has been changed.

Parameters:

  1. ConnectionState connectionState - the updated connection state

Static Readonly memberJoined

memberJoined: "memberJoined" = 'memberJoined'

Fired when a member has joined a channel.

Parameters:

  1. Member member - the member in question

Static Readonly memberLeft

memberLeft: "memberLeft" = 'memberLeft'

Fired when a member has left a channel.

Parameters:

  1. Member member - the member in question

Static Readonly memberUpdated

memberUpdated: "memberUpdated" = 'memberUpdated'

Fired when a member's fields have been updated.

Parameters:

  1. object data - info object provided with the event. It has the following properties:

Static Readonly messageAdded

messageAdded: "messageAdded" = 'messageAdded'

Fired when a new message has been added to the channel on the server.

Parameters:

  1. Message message - the message in question

Static Readonly messageRemoved

messageRemoved: "messageRemoved" = 'messageRemoved'

Fired when a message is removed from the message list of a channel.

Parameters:

  1. Message message - the message in question

Static Readonly messageUpdated

messageUpdated: "messageUpdated" = 'messageUpdated'

Fired when the fields of an existing message are updated with new values.

Parameters:

  1. object data - info object provided with the event. It has the following properties:

Static Readonly pushNotification

pushNotification: "pushNotification" = 'pushNotification'

Fired when the client has received (and parsed) a push notification via one of the push channels (apn or fcm).

Parameters:

  1. PushNotification pushNotification - the push notification in question

Static Readonly stateChanged

stateChanged: "stateChanged" = 'stateChanged'

Fired when the state of the client has been changed.

Parameters:

  1. State state - the new client state

Static Readonly tokenAboutToExpire

tokenAboutToExpire: "tokenAboutToExpire" = 'tokenAboutToExpire'

Fired when the token is about to expire and needs to be updated.

  • Parameters:
  1. number ttl - token's time to live

Static Readonly tokenExpired

tokenExpired: "tokenExpired" = 'tokenExpired'

Fired when the token has expired.

Static Readonly typingEnded

typingEnded: "typingEnded" = 'typingEnded'

Fired when a member has stopped typing.

Parameters:

  1. Member member - the member in question

Static Readonly typingStarted

typingStarted: "typingStarted" = 'typingStarted'

Fired when a member has started typing.

Parameters:

  1. Member member - the member in question

Static Readonly userSubscribed

userSubscribed: "userSubscribed" = 'userSubscribed'

Fired when the client has subscribed to a user.

Parameters:

  1. User user - the user in question

Static Readonly userUnsubscribed

userUnsubscribed: "userUnsubscribed" = 'userUnsubscribed'

Fired when the client has unsubscribed from a user.

Parameters:

  1. User user - the user in question

Static Readonly userUpdated

userUpdated: "userUpdated" = 'userUpdated'

Fired when the properties or the reachability status of a user have been updated.

Parameters:

  1. object data - info object provided with the event. It has the following properties:
    • User user - the user in question
    • UserUpdateReason[] updateReasons - array of reasons for the update

Properties

connectionState

connectionState: ConnectionState = 'unknown'

Client connection state.

Readonly version

version: string = ...

Current version of the Chat client.

Static Readonly version

version: string = ...

Current version of the Chat client.

Accessors

reachabilityEnabled

  • get reachabilityEnabled(): boolean
  • Client reachability state. Throws if accessed before the client initialization was completed.

    Returns boolean

user

  • Information of the logged-in user. Before client initialization, returns an uninitialized user. Will trigger a Client.userUpdated event after initialization.

    Returns User

Methods

createChannel

  • Create a channel on the server and subscribe to its events. The default is a public channel with an empty friendly name.

    Parameters

    Returns Promise<Channel>

getChannelBySid

  • getChannelBySid(channelSid: string): Promise<Channel>
  • Get a known channel by its SID.

    Parameters

    • channelSid: string

      Channel sid

    Returns Promise<Channel>

getChannelByUniqueName

  • getChannelByUniqueName(uniqueName: string): Promise<Channel>
  • Get a known channel by its unique identifier name.

    Parameters

    • uniqueName: string

      The unique identifier name of the channel.

    Returns Promise<Channel>

getLocalChannels

  • Get an array of channels that are locally known to the client in the provided sorting order. Locally known channels are the ones that are created and/or joined during the client runtime and all the subscribed channel of the currently-logged-in user. To ensure that the full list of the subscribed channels has fetched, call the Client.getSubscribedChannels method and fetch all the pages with the help of the RestPaginator.nextPage method.

    Parameters

    Returns Promise<Channel[]>

getPublicChannelDescriptors

  • Get the content of the public channels directory.

    Returns Promise<Paginator<ChannelDescriptor>>

getSubscribedChannels

  • getSubscribedChannels(args?: any): Promise<Paginator<Channel>>
  • Get the current list of all the subscribed channels.

    Parameters

    • Optional args: any

    Returns Promise<Paginator<Channel>>

getSubscribedUsers

  • getSubscribedUsers(): Promise<User[]>
  • Get a list of subscribed user objects.

    Returns Promise<User[]>

getUser

  • getUser(identity: string): Promise<User>
  • Gets a user with the given identity. If it's in the subscribed list, then return the user object from it; if not, then subscribe and add user to the subscribed list.

    Parameters

    • identity: string

      Identity of the user.

    Returns Promise<User>

    A fully initialized user.

getUserChannelDescriptors

  • Get the content of the channels directory of the user (created by, joined or invited to).

    Returns Promise<Paginator<ChannelDescriptor>>

getUserDescriptor

  • Gets the user descriptor for given identity.

    Parameters

    • identity: string

      Identity of the user.

    Returns Promise<UserDescriptor>

    The user descriptor.

handlePushNotification

  • handlePushNotification(notificationPayload: any): Promise<void>
  • Handle push notification payload parsing and emit the Client.pushNotification event on this Client instance.

    Parameters

    • notificationPayload: any

      Push notification payload

    Returns Promise<void>

setPushRegistrationId

  • setPushRegistrationId(channelType: ChannelType, registrationId: string): Promise<void>
  • Register for push notifications.

    Parameters

    • channelType: ChannelType

      Channel type.

    • registrationId: string

      Push notification ID provided by the platform.

    Returns Promise<void>

shutdown

  • shutdown(): Promise<void>
  • Gracefully shut down the client.

    Returns Promise<void>

unsetPushRegistrationId

  • unsetPushRegistrationId(channelType: ChannelType): Promise<void>
  • Unregister from push notifications.

    Parameters

    • channelType: ChannelType

      Channel type.

    Returns Promise<void>

updateToken

  • updateToken(token: string): Promise<Client>
  • Update the token used by the client and re-register with the Programmable Chat services.

    Parameters

    • token: string

      New access token.

    Returns Promise<Client>

Static parsePushNotification

  • Static method for push notification payload parsing. Returns parsed push as a PushNotification object.

    Parameters

    • notificationPayload: any

      Push notification payload.

    Returns PushNotification