Skip to content

Commit e8acd3d

Browse files
committed
docs(readme): update doc
1 parent 29e3268 commit e8acd3d

File tree

6 files changed

+102
-2
lines changed

6 files changed

+102
-2
lines changed

README.md

Lines changed: 102 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
1+
<h1 align="center">
2+
<img src="https://github.com/Gapur/react-native-twilio-chat/blob/master/src/assets/twilio-logo.png" />
3+
<br/>
4+
React Native Twilio Chat
5+
</h1>
6+
17
<p align="center">
28
<img width="600"src="https://github.com/Gapur/react-native-twilio-chat/blob/master/src/assets/example.gif">
39
</p>
410

5-
# React Native Twilio Chat
6-
711
Build a Chat App with Twilio and React-Native
812

913
Best Practices Using Twilio Programmable Chat
1014

1115
Twilio Programmable Chat makes it easy for you to add chat features into your web and mobile apps without building or scaling a real-time chat backend. Chat has all the necessary APIs and features to integrate with your business logic to ensure you are in control.
1216

17+
I wanted to build a quick, full-featured chat feature for my React Native app. I managed to do it with [Twilio Programmable Chat](https://www.twilio.com/docs/chat).
18+
19+
I searched the internet a lot to find the best way to use Twilio Programmable Chat with React Native. Unfortunately, I couldn’t find much. So I decided to write an article about it, hopefully saving others some time.
20+
1321
## Setting up the Project
1422

1523
Install the repository:
@@ -23,6 +31,98 @@ cd react-native-twilio-chat
2331
npm run ios
2432
```
2533

34+
## Creating Our Server
35+
36+
Before we get started, We need to generate an access token to authorize the React Native app to communicate with the Programmable Twilio Chat.
37+
To set up our backend for Chat, we’ll need four values from our Twilio account. We’ll store these in our .env file:
38+
- Service instance SID—a service instance where all the data for our application is stored and scoped
39+
- Account SID — your primary Twilio account identifier
40+
- API key — used to authenticate
41+
- API secret — used to authenticate
42+
43+
Now, if your account is ready, you can find your account SID on the [Twilio Console](https://www.twilio.com/console). You should copy and paste it as the value TWILIO_ACCOUNT_SID to the .env file.
44+
45+
<p align="center">
46+
<img width="800"src="https://github.com/Gapur/react-native-twilio-chat/blob/master/src/assets/account_sid.png">
47+
</p>
48+
49+
Next, We need to generate an API key and API secret by navigating to Settings > API Keys > New API Key.
50+
51+
<p align="center">
52+
<img width="800"src="https://github.com/Gapur/react-native-twilio-chat/blob/master/src/assets/new_api_key.png">
53+
</p>
54+
55+
If you create these things successfully, let’s copy and paste the SID and secret as the values TWILIO_API_KEY and TWILIO_API_SECRET.
56+
57+
<p align="center">
58+
<img width="800"src="https://github.com/Gapur/react-native-twilio-chat/blob/master/src/assets/sid_and_secret_key.png">
59+
</p>
60+
61+
Last, we need to create a new Chat Service by navigating to All Products & Services > Programmable Chat > Services > Chat Services.
62+
63+
<p align="center">
64+
<img width="800"src="https://github.com/Gapur/react-native-twilio-chat/blob/master/src/assets/chat_service.png">
65+
</p>
66+
67+
Let’s copy and paste the service SID as the value TWILIO_CHAT_SERVICE_SID.
68+
69+
Finally, our .env file should look like this:
70+
```js
71+
TWILIO_ACCOUNT_SID=your_account_sid
72+
TWILIO_API_KEY=your_api_key
73+
TWILIO_API_SECRET=your_api_secret
74+
TWILIO_CHAT_SERVICE_SID=your_chat_service_sid
75+
```
76+
77+
When our .env is ready, we can create a simple server with a single GET route, /token/:identity. This route will request and return a token from TWILIO. Let’s install dependencies for our server:
78+
79+
```sh
80+
yarn add express dotenv twilio
81+
```
82+
83+
Create our server.js:
84+
```js
85+
require('dotenv').config();
86+
87+
const Twilio = require('twilio');
88+
const express = require('express');
89+
90+
const app = express();
91+
92+
const AccessToken = Twilio.jwt.AccessToken;
93+
const ChatGrant = AccessToken.ChatGrant;
94+
95+
app.get('/token/:identity', (req, res) => {
96+
const identity = req.params.identity;
97+
const token = new AccessToken(
98+
process.env.TWILIO_ACCOUNT_SID,
99+
process.env.TWILIO_API_KEY,
100+
process.env.TWILIO_API_SECRET,
101+
);
102+
103+
token.identity = identity;
104+
token.addGrant(
105+
new ChatGrant({
106+
serviceSid: process.env.TWILIO_CHAT_SERVICE_SID,
107+
}),
108+
);
109+
110+
res.send({
111+
identity: token.identity,
112+
jwt: token.toJwt(),
113+
});
114+
});
115+
116+
app.listen(3001, function () {
117+
console.log('Programmable Chat server running on port 3001!');
118+
});
119+
```
120+
121+
That’s it for our server. Now, We can run our server with the following command line:
122+
```sh
123+
node server.js
124+
```
125+
26126
## How to contribute?
27127

28128
1. Fork this repo

src/assets/account_sid.png

227 KB
Loading

src/assets/chat_service.png

129 KB
Loading

src/assets/new_api_key.png

130 KB
Loading

src/assets/sid_and_secret_key.png

154 KB
Loading

src/assets/twilio-logo.png

15.8 KB
Loading

0 commit comments

Comments
 (0)