Programmable Chat has been deprecated and is no longer supported. Instead, we'll be focusing on the next generation of chat: Twilio Conversations. Find out more about the EOL process here.
If you're starting a new project, please visit the Conversations Docs to begin. If you've already built on Programmable Chat, please visit our Migration Guide to learn about how to switch.
AccessManager is no longer a required, direct dependency. This changes the way you init your Chat client:
The old way:
1let accessManager = new Twilio.Common.AccessManager(token);2let chatClient = new Twilio.Chat.Client(accessManager);
The new way just requires the token to be passed:
1let chatClient = new Twilio.Chat.Client(token);2
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:
1let accessManager = new Twilio.AccessManager(token);2let chatClient = new Twilio.Chat.Client(token);34accessManager.on('tokenUpdated', () => {5chatClient.updateToken(accessManager.token);6});78accessManager.on('tokenExpired', () => {9let newToken = obtainNewToken();10accessManager.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 succeededchannel.messages
: In previous versions you could access the list of fetched messages for the channel through the synchronous property channel.messages
.channel.getMessages()
. Don't worry, we will cache messages for you, so it won't make the performance of your app any worse.In the previous version to get a user channels list, you had to do something like the following:
1chatClient.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:
Let's see how it works:
Just populate a list of my channels:
1// Function accepts a channels page, which has an array of items and a method `nextPage` to proceed2// to next page, if it exists3function populateChannels(channelsPage) {4channelsPage.items.forEach(channel => drawChannelSomehow);5if(channelsPage.hasNextPage) {6channelsPage.nextPage().then(populateChannels);7}8}910chatClient.getUserChannels().then(populateChannels);
Get a public channels list:
1chatClient.getPublicChannels().then(channelDescriptorPage => {2// First page of channel descriptor3// Paging works the same way as for user channels4channelDescriptorPage.items.forEach(descriptor => {5console.log('Descriptor for a channel:', descriptor.friendlyName, descriptor.sid);6})7});
Join to the channel from public list
1channelDescriptor.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
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:
1// Get Messages for a previously created channel2myChannel.getMessages()3.then(function(messagesPage) {4messagesPage.items.forEach(message => {5console.log('Author:' + message.author);6});7if (messagesPage.hasNextPage) {8return messagesPage.nextPage();9}10});
The anchor
argument for getMessages
has changed from a String representing a Message sid
, to an integer representing the Message index
One of the most significant new features is an a set of counter methods: channel.getMembersCount(), channel.getMessagesCount() and channel.getUnconsumedMessagesCount().
Note: These new methods are currently semi-realtime. This means that this data will be eventually correct, but will also possibly be incorrect for a few seconds. The Chat system does not provide real time events for counter values 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.