In your Programmable Chat application, you will be responsible for two components:
- A client (browser, iOS, Android) app where users send and receive messages
- A server which vouches for the identity of your users
This guide will show you how these two pieces work together in your messaging application.
When a user in their browser or on their mobile device wants to start sending and receiving messages, Twilio can't trust them right away. Not on this Internet! Twilio needs YOU the developer to vouch for your users, so we can trust them too.
You'll need to give your users an Access Token that tells Twilio who they are and what they can do. In our starter apps, the client requests an Access Token from the server like so.
Clients should provide a device ID of some kind to uniquely identify which device they are connecting
from. You'll probably want to use vendor ID on iOS
ANDROID_ID on Android.
In web applications, there's not a great built-in solution for this, but you might
consider using a library like fingerprint.js.
Let's see how we handle this request on the server and create a token for the client.
On the server we must decide, based on the token request that was sent to us, who the user is and what they should be allowed to do.
To figure out who the user is (their identity), you might use your existing login system (using session cookies, an API token, or whatever mechanism you use to secure API requests or pages today). You might not care who a user is at all, and assign them a temporary identity as in our starter apps. Who the user is and how you determine that will vary from app to app.
If you determine that the user should indeed be allowed to access your Chat application, you must grant your user access to Chat and configure an Endpoint ID. Here's how we generate those tokens in our starter apps.
An "endpoint" in Chat is a unique app, device, and user combination that can receive a message. For example, "email@example.com" using "SquareChat" on her iPhone is a different message delivery destination (endpoint) than "firstname.lastname@example.org" using "SquareChat" on her Kindle Fire tablet. The endpoint ID you generate on the server - while it can be a string in any format you want - should integrate at least these three dimensions.
Once your client receives an Access Token from your server, you can initialize the Twilio Chat SDK and start sending and receiving messages.
Access Token in hand, we can now initialize the Chat SDK on the client to start doing fun stuff like sending and receiving messages. Here's how you would use the token string to initialize the client, again taken from the starter apps.
Now that your token logic is setup, the first thing you're likely to want to do is send and receive messages on a channel. Press on to learn how to make that happen!