Skip to contentSkip to navigationSkip to topbar
Page tools
Useful for sharing or LLM
Accelerate development with AI

On this page
Looking for more inspiration?Visit the

Migration Guide: JavaScript 0.11.0


(error)

Programmable Chat deprecated

Programmable Chat has been deprecated and receives no support. Twilio has turned its focus to the next generation of chat: Twilio Conversations. Find out more about the EOL process(link takes you to an external page).

  • To start a project, see Conversations.
  • To switch from Programmable Chat, see the Migration Guide to learn about how to switch.

Client Instantiation

client-instantiation page anchor

AccessManager is no longer a required, direct dependency. This changes the way you init your Chat client:

The old way:

Old: instantiate the client with an AccessManager

old-instantiate-the-client-with-an-accessmanager page anchor
1
let accessManager = new Twilio.Common.AccessManager(token);
2
let chatClient = new Twilio.Chat.Client(accessManager);

The new way just requires the token to be passed:

New: instantiate the client with a token

new-instantiate-the-client-with-a-token page anchor
let chatClient = new Twilio.Chat.Client(token);

This does also mean that you have to call the chatClient.updateToken(token) method when needed, rather than using the old AccessManager.

If you still wish to use the convenience AccessManager to help track token lifecycle events, you can use an AccessManager to track a token lifetime if needed. The AccessManager is still found in the Twilio Common library.

The new version of AccessManager would be used as follows:

Track token lifecycle with AccessManager

track-token-lifecycle-with-accessmanager page anchor
1
let accessManager = new Twilio.AccessManager(token);
2
let chatClient = new Twilio.Chat.Client(token);
3
4
accessManager.on('tokenUpdated', () => {
5
chatClient.updateToken(accessManager.token);
6
});
7
8
accessManager.on('tokenExpired', () => {
9
let newToken = obtainNewToken();
10
accessManager.updateToken(newToken);
11
});

  • client.identity : Now you can access this with client.userInfo.identity. Note that you may access any properties only after the client.initialize() call has succeeded
  • channel.messages : In previous versions you could access the list of fetched messages for the channel through the synchronous property channel.messages .
  • With the new version you will need to always call channel.getMessages() . Don't worry, we will cache messages for you, so it won't make the performance of your app any worse.

Separated Public and User Channel Lists

separated-public-and-user-channel-lists page anchor

In the previous version to get a user channels list, you had to do something like the following:

Old: filter joined and invited channels

old-filter-joined-and-invited-channels page anchor
1
chatClient.getChannels()
2
.filter(channel => (channel.status === 'joined' || channel.status === 'invited'))
3
.then(result => doSomething);

That approach had multiple issues related to client initialization latency, consumed traffic, etc., so we made a couple of changes:

  • User channels (ones which you joined, invited, and your private channels), and public channels lists are separated
  • Public channels list consists of special "channelDescriptor" objects, which are much more lightweight so handling a huge catalogue of public channels will be much easier
  • We have added a pagination to load channels bit-by-bit

Let's see how it works:

Just populate a list of my channels:

Populate the user channels list with pagination

populate-the-user-channels-list-with-pagination page anchor
1
// Function accepts a channels page, which has an array of items and a method `nextPage` to proceed
2
// to next page, if it exists
3
function populateChannels(channelsPage) {
4
channelsPage.items.forEach(channel => drawChannelSomehow);
5
if(channelsPage.hasNextPage) {
6
channelsPage.nextPage().then(populateChannels);
7
}
8
}
9
10
chatClient.getUserChannels().then(populateChannels);

Get a public channels list:

Get the public channels list

get-the-public-channels-list page anchor
1
chatClient.getPublicChannels().then(channelDescriptorPage => {
2
// First page of channel descriptor
3
// Paging works the same way as for user channels
4
channelDescriptorPage.items.forEach(descriptor => {
5
console.log('Descriptor for a channel:', descriptor.friendlyName, descriptor.sid);
6
})
7
});

Join to the channel from public list

Join a channel from the public list

join-a-channel-from-the-public-list page anchor
1
channelDescriptor.getChannel()
2
.then(channel => channel.join())
3
.then(channel => { console.log('Joined to the channel', channel.friendlyName);

Also note, that "channelAdded" and "channelRemoved" will be fired only for channels from "User channels" list, not for the public one

Channel getMessages returns a paginator

channel-getmessages-returns-a-paginator page anchor

We have standardized paging and as such paged resource (currently Message) lists will return a Paginator, not an array as previously.

This now looks as follows:

Get messages as a paginator

get-messages-as-a-paginator page anchor
1
// Get Messages for a previously created channel
2
myChannel.getMessages()
3
.then(function(messagesPage) {
4
messagesPage.items.forEach(message => {
5
console.log('Author:' + message.author);
6
});
7
if (messagesPage.hasNextPage) {
8
return messagesPage.nextPage();
9
}
10
});

Anchor for Message Paging Changed Type

anchor-for-message-paging-changed-type page anchor

The anchor argument for getMessages has changed from a String representing a Message sid, to an integer representing the Message index


Count methods for messages and members

count-methods-for-messages-and-members page anchor

A set of counter methods is added: channel.getMembersCount(), channel.getMessagesCount(), and channel.getUnconsumedMessagesCount().

Note: These methods are semi-realtime. The data is eventually correct, but may be incorrect for a few seconds, because the Chat system does not provide real-time events for counter value changes.

So this is quite useful for any "unread messages count" badges, but we would not recommend any core application logic based on these counters being accurate in real time.