SDK Overview
Twilio provides a client-side SDK for browser-based development, as well as SDKs for native development on iOS and Android.
Our SDKs provide a convenient collection of objects, methods, and events to connect your client-side application to Conversations (classic).
For installation methods, version history, and reference documentation, see:
- The JavaScript, Android, or iOS download page
- The JavaScript, Android, or iOS changelogs
- The JavaScript, Android, or iOS auto-generated documentation
To initialize the Conversations (classic) SDK, create a new Client object and pass a valid Access Token to the client creation method as the first parameter.
After that, listen for the client to inform you when it's fully initialized and synchronized. Once you receive this confirmation, the client is ready to use.
1/* Initialization */2import { Client } from '@twilio/conversations';34const client = new Client('token');5client.on('stateChanged', (state) => {6if (state === 'failed') {7// The client failed to initialize8return;9}1011if (state === 'initialized') {12// Use the client13}14});
Info
If the token expires before you renew it, the client's connection state will change to disconnected, and you'll need to initialize a new client object.
All tokens have a limited lifetime to protect you from abuse. The maximum and default lifetime is 24 hours, but you should make it as short as possible for your application. Therefore, you may need to renew the token during your SDK session. The SDK will notify you when the token is "about to expire" and when it "has expired".
To avoid needing to instantiate a new client, you should get a new token from your server and pass it to the client's updateToken method before the old one expires. This method will update the authentication token for your client and re-register with the Conversations (classic) services.
1/* Handling token expiration/expiration warning events */23client.on("tokenAboutToExpire", (time) => {4// token is about to expire. get a new token5try {6const token = (await fetch("https://placekitten.com/getToken?username=username&password=password")).data();7} catch {8return Error("Unable to get a token");9}1011// update the client with new token12client = await client.updateToken(token);1314// use updated client15});1617client.on("tokenExpired", () => {18// get a new token19try {20const token = (await fetch("https://placekitten.com/getToken?username=username&password=password")).data();21} catch {22return Error("Unable to get a token");23}2425// token expired. create a new client26client = new Client(token);27});2829// update the token used by the client and re-register with the Conversations services30await client.updateToken("token");
Info
There is a reconnection attempt period when the network connectivity is lost before the client switches to the disconnected state.
During use, the connection state of your SDK client may change.
These are the possible client connection states:
connecting- the client is offline and a connection attempt is in progressconnected- the client is online and readydisconnecting- the client is going offline as disconnection is in progressdisconnected- the client is offline and no connection attempt is in progressdenied- the client connection is denied because of an invalid JSON Web Token access token. The user must refresh the token in order to proceed
These states are also documented in the SDK reference docs.
Client state changes are due to different factors. For example, the disconnected state can result from a network disruption, an expired token, or another error. You can listen to the client's connection state events to detect this and respond accordingly.
As you build your Conversations (classic) application, you might find it helpful to check the Twilio Console debugger. This service aggregates all additional errors or warnings that may be triggered from Twilio's webhooks to your server, as well as token errors.
You can also enable debug logging by passing an option for increased log verbosity to your client when you create it. Check the auto-generated docs or Error Handling and Diagnostics for platform-specific examples.
After configuring your Conversations (classic) SDK client, you can:
- Learn how to handle events, or
- Check out our Working with Conversations (classic) guide