Options
All
  • Public
  • Public/Protected
  • All
Menu

Twilio Chat client library

Twilio Chat is a service for messages delivery over ip networks. Visit our official site for more detalis: https://www.twilio.com/chat

Instantiating and using

To use the library you need to generate a token and pass it to the Chat Client constructor.

NPM

npm install --save twilio-chat

Using this method, you can require twilio-chat and then use the client:

const { Client } = require('twilio-chat');
const client = new Client(token);
// Before you use the client, subscribe to the `'stateChanged'` event and wait 
// for the `'initialized'` state to be reported.
client.on('stateChanged', (state) => {
  if (state === 'initialized') {
    // Use the client
  }
}

The SDK could also be imported using the ES module syntax:

import { Client } from 'twilio-chat';
const client = new Client(token);
// Before you use the client, subscribe to the `'stateChanged'` event and wait 
// for the `'initialized'` state to be reported.
client.on('stateChanged', (state) => {
  if (state === 'initialized') {
    // Use the client
  }
}

CDN

Releases of twilio-chat.js are hosted on a CDN, and you can include these directly in your web app using a <script> tag.

<script src="https://media.twiliocdn.com/sdk/js/chat/v6.0/twilio-chat.min.js"></script>

Using this method, twilio-chat.js will set a browser global Twilio.Chat through which you can use the client:

const client = new Twilio.Chat.Client(token);

Supported Browsers

BrowserSupported Versions
Chrome for Android94
Firefox for Android92
UC Browser for Android12.12
Chrome94, 93, 92
Edge94, 93, 92
Firefox93, 92, 91
Internet Explorer11
Safari for iOS15, 14.5-14.8
Safari15, 14.1, 14
Samsung Internet15.0, 14.0

Changelog

See this link.

Index

Type aliases

ChannelSortingCriteria

ChannelSortingCriteria: "lastMessage" | "friendlyName" | "uniqueName"

Channel sorting criteria.

ChannelSortingOrder

ChannelSortingOrder: "ascending" | "descending"

Channel sorting order.

ChannelState

ChannelState: { current: "active" | "inactive" | "closed"; dateUpdated: Date }

The conversational state of the channel.

Type declaration

  • current: "active" | "inactive" | "closed"

    The current state.

  • dateUpdated: Date

    Date at which the latest channel state update happened.

ChannelStatus

ChannelStatus: "unknown" | "notParticipating" | "invited" | "joined"

The status of the channel, relative to the client: whether the channel has been joined or the client is notParticipating in the channel or has been invited to the channel.

ChannelType

ChannelType: "public" | "private"

Channel type.

ChannelUpdateReason

ChannelUpdateReason: "attributes" | "createdBy" | "dateCreated" | "dateUpdated" | "friendlyName" | "lastConsumedMessageIndex" | "state" | "status" | "uniqueName" | "lastMessage" | "notificationLevel"

The reason for the updated event being emitted by a channel.

ConnectionState

ConnectionState: TwilsockConnectionState

Connection state of the client. Possible values are as follows:

  • 'connecting' - client is offline and connection attempt is in process
  • 'connected' - client is online and ready
  • 'disconnecting' - client is going offline as disconnection is in process
  • 'disconnected' - client is offline and no connection attempt is in process
  • 'denied' - client connection is denied because of invalid JWT access token. User must refresh token in order to proceed

MemberType

MemberType: "chat" | "sms" | "whatsapp"

Push notification type of a member.

MemberUpdateReason

MemberUpdateReason: "attributes" | "dateCreated" | "dateUpdated" | "roleSid" | "lastConsumedMessageIndex" | "lastConsumptionTimestamp"

The reason for the updated event being emitted by a member.

MessageType

MessageType: "text" | "media"

Push notification type of a message.

MessageUpdateReason

MessageUpdateReason: "body" | "lastUpdatedBy" | "dateCreated" | "dateUpdated" | "attributes" | "author"

The reason for the updated event being emitted by a message.

NotificationLevel

NotificationLevel: "default" | "muted"

The user's notification level for the channel. Determines whether the currently logged-in user will receive pushes for events in this channel. Can be either muted or default, where default defers to the global service push configuration.

NotificationsChannelType

NotificationsChannelType: ChannelType

Notifications channel type. Possible values are as follows:

  • 'fcm'
  • 'apn'

State

State: "failed" | "initialized"

State of the client. Possible values are as follows:

  • 'failed' - the client failed to initialize
  • 'initialized' - the client successfully initialized

UserUpdateReason

UserUpdateReason: "friendlyName" | "attributes" | "online" | "notifiable"

The reason for the updated event being emitted by a user.