Ready to implement a chat application using Twilio Programmable Chat Client?
This application allows users to exchange messages through different channels, using the Twilio Programmable Chat API. On this example, we'll show how to use this API capabilities to manage channels and their usages.
In order to create a Twilio Programmable Chat client, you will need an access token. This token holds information about your Twilio Account and Chat API keys.
We generate this token by creating a new
AccessToken and providing it with an
ChatGrant. With the
AccessToken at hand, we can use its method
ToJWT() to return its string representation.
We can generate a token, now we need a way for the chat app to get it.
On our controller we expose the endpoint responsible for providing a valid token. Using the information provided as parameters:
device: identifies the device being used.
identity: identifies the user itself.
tokenGenerator.Generate method to get hold of a new token and return it in a JSON format to be used for our client.
Now that we have a route that generates JWT tokens on demand, let's use this route to initialize our Twilio Chat Client.
On our client, we fetch a new Token using a POST request to our endpoint.
With the token we can create a new
Twilio.AccessManager, and initialize our
Now that we've initialized our Chat Client, lets see how we can get a list of channels.
Next, we need a default channel.
This application will try to join a channel called "General Channel" when it starts. If the channel doesn't exist, we'll create one with that name. The scope of this example application will show you how to work only with public channels, but the Programmable Chat client allows you to create private channels and handles invitations.
Notice we set a unique name for the general channel as we don't want to create a new general channel every time we start the application.
Now let's listen for some channel events.
Next we listen for channel events. In our case, we're setting listeners to the following events:
messageAdded: When another member sends a message to the channel you are connected to.
typingStarted: When another member is typing a message on the channel that you are connected to.
typingEnded: When another member stops typing a message on the channel that you are connected to.
memberJoined: When another member joins the channel that you are connected to.
memberLeft: When another member leaves the channel that you are connected to.
We register a different function to handle each particular event.
The client emits events as well. Let's see how we can listen to those events as well.
We've actually got a real chat app going here, but let's make it more interesting with multiple channels.
When a user clicks on the "+ Channel" link we'll show an input text field where it's possible to type the name of the new channel. Creating a channel is as simple as calling
createChannel with an object that has the
friendlyName key. You can create a channel with more options listed on the Channels section of the Programmable Chat documentation.
Next, we will see how we can switch between channels.
When you tap on the name of a channel from the sidebar, that channel is set as the
selectChannel method takes care of joining to the selected channel and setting up the
At some point your users will want to delete a channel. Let's have a look at how that can be done.
Deleting a channel is even more simple than creating one. The application lets the user delete the channel they are currently joined to through the "delete current channel" link. The only thing you need to do to actually delete the channel from Twilio, is call the
delete method on the channel you are trying to delete. Like other methods on the
Channel object, it'll return a promise where you can set the success handler.