Menu

Rate this page:

Thanks for rating this page!

We are always striving to improve our documentation quality, and your feedback is valuable to us. How could this documentation serve you better?

Migration Guide: JavaScript 0.11.0

Client Instantiation

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

The old way:

    let accessManager = new Twilio.Common.AccessManager(token);
    let chatClient = new Twilio.Chat.Client(accessManager);

The new way just requires the token to be passed:

    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 lifecyle 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:

    let accessManager = new Twilio.AccessManager(token);
    let chatClient = new Twilio.Chat.Client(token);

    accessManager.on('tokenUpdated', () => {
        chatClient.updateToken(accessManager.token);
    });

    accessManager.on('tokenExpired', () => {
        let newToken = obtainNewToken();
        accessManager.updateToken(newToken);
    });

Removed Properties

  • 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

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

    chatClient.getChannels()
      .filter(channel => (channel.status === 'joined' || channel.status === 'invited'))
      .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:

    // Function accepts a channels page, which has an array of items and a method `nextPage` to proceed 
    // to next page, if it exists
    function populateChannels(channelsPage) {
      channelsPage.items.forEach(channel => drawChannelSomehow);
      if(channelsPage.hasNextPage) {
        channelsPage.nextPage().then(populateChannels);
      }
    }

    chatClient.getUserChannels().then(populateChannels);

Get a public channels list:

    chatClient.getPublicChannels().then(channelDescriptorPage => {
      // First page of channel descriptor
      // Paging works the same way as for user channels
      channelDescriptorPage.items.forEach(descriptor => {
        console.log('Descriptor for a channel:', descriptor.friendlyName, descriptor.sid);
      })
    });

Join to the channel from public list

    channelDescriptor.getChannel()
      .then(channel => channel.join())
      .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 Now Returns a Paginator Rather Than an Array

We have standarized 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 for a previously created channel
myChannel.getMessages()
  .then(function(messagesPage) {
     messagesPage.items.forEach(message => {
        console.log('Author:' + message.author);
     });
     if (messagesPage.hasNextPage) {
        return messagesPage.nextPage();
     }
});

Anchor for Message Paging Changed Type

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

New Features

New Count Methods for Messages and Members

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.

Need some help?

We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd browsing the Twilio tag on Stack Overflow.

Loading Code Sample...