Skip to content

Commit e5d802d

Browse files
author
timmydoza
authored
Merge pull request #40 from twilio-labs/support-room-type
Support room type
2 parents c25eecc + 57006d6 commit e5d802d

File tree

10 files changed

+311
-80
lines changed

10 files changed

+311
-80
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## 0.3.0
4+
5+
### Enhancements
6+
7+
- This version adds the ability to specify the room type when deploying a video application token server. Use the `--room-type` flag when deploying the app. For example: `twilio rtc:apps:video:deploy --authentication passcode --room-type group-small`. The payload returned by the token server will also include the room type (see the [readme](README.md) for details). The `--room-type` flag is optional and the default value is `group`.
8+
39
## 0.2.0
410

511
### Breaking Change

README.md

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,12 @@ POST /token
9090

9191
###### Parameters
9292

93-
| Name | Type | Description |
94-
| --------------- | -------- | -------------------------------------------------------------------------------------- |
95-
| `passcode` | `string` | **Required**. The application passcode. |
96-
| `user_identity` | `string` | **Required**. The user's identity. |
97-
| `room_name` | `string` | A room name that will be used to create a token scoped to connecting to only one room. |
93+
| Name | Type | Description |
94+
| --------------- | --------- | -------------------------------------------------------------------------------------- |
95+
| `passcode` | `string` | **Required**. The application passcode. |
96+
| `user_identity` | `string` | **Required**. The user's identity. |
97+
| `room_name` | `string` | A room name that will be used to create a token scoped to connecting to only one room. |
98+
| `create_room` | `boolean` | (default: `true`) When false, a room will not be created when a token is requested. |
9899

99100
###### Success Responses
100101

@@ -108,7 +109,8 @@ POST /token
108109

109110
```json
110111
{
111-
"token": "0000000000000000.0000000000000000000000.00000000000000000"
112+
"token": "0000000000000000.0000000000000000000000.00000000000000000",
113+
"room_type": "group" | "group-small" | "peer-to-peer" | null
112114
}
113115
```
114116

@@ -172,6 +174,19 @@ POST /token
172174
</td>
173175
</tr>
174176

177+
<tr>
178+
<td> 400 </td>
179+
<td>
180+
181+
```json
182+
{
183+
"error": {
184+
"message": "invalid parameter",
185+
"explanation": "A boolean value must be provided for the create_room parameter"
186+
}
187+
}
188+
```
189+
175190
</table>
176191

177192
## Commands
@@ -208,11 +223,12 @@ USAGE
208223
$ twilio rtc:apps:video:deploy --authentication <auth>
209224
210225
OPTIONS
211-
-l=(debug|info|warn|error|none) [default: info] Level of logging messages.
212-
-p, --profile=profile Shorthand identifier for your profile.
213-
--app-directory=app-directory Name of app directory to use
214-
--authentication=(passcode) (required) Type of authentication to use
215-
--override Override an existing App deployment
226+
-l=(debug|info|warn|error|none) [default: info] Level of logging messages.
227+
-p, --profile=profile Shorthand identifier for your profile.
228+
--app-directory=app-directory Name of app directory to use
229+
--authentication=(passcode) (required) Type of authentication to use
230+
--override Override an existing App deployment
231+
--room-type=(group|group-small|peer-to-peer) [default: group] Set room type
216232
217233
DESCRIPTION
218234
This command publishes two components as a Twilio Function: an application token
@@ -233,21 +249,33 @@ EXAMPLES
233249
$ twilio rtc:apps:video:deploy --authentication passcode
234250
deploying app... done
235251
Passcode: xxx xxx xxxx xxxx
252+
Expires: Mon Mar 09 2020 16:36:23 GMT-0600
253+
Room Type: group
236254
237255
# Deploy an application token server with the React app
238256
$ twilio rtc:apps:video:deploy --authentication passcode --app-directory /path/to/app
239257
deploying app... done
240258
Web App URL: https://video-app-xxxx-xxxx-dev.twil.io?passcode=xxxxxxxxxxxxxx
241259
Passcode: xxx xxx xxxx xxxx
260+
Expires: Mon Mar 09 2020 16:36:23 GMT-0600
261+
Room Type: group
242262
243263
# Override an existing app with a fresh deployment
244264
# Please note that this will remove a previously deployed web application if no
245265
# app directory is provided
246266
$ twilio rtc:apps:video:deploy --authentication passcode --override
247267
Removed app with Passcode: xxx xxx xxxx xxxx
248268
deploying app... done
269+
Passcode: yyy yyy yyyy yyyy
270+
Expires: Mon Mar 09 2020 16:36:23 GMT-0600
271+
Room Type: group
272+
273+
# Deploy an application token server with a specific room type
274+
$ twilio rtc:apps:video:deploy --authentication passcode --room-type peer-to-peer
275+
deploying app... done
249276
Passcode: xxx xxx xxxx xxxx
250277
Expires: Mon Mar 09 2020 16:36:23 GMT-0600
278+
Room Type: peer-to-peer
251279
```
252280

253281
## `twilio rtc:apps:video:view`
@@ -266,6 +294,7 @@ EXAMPLE
266294
$ twilio rtc:apps:video:view
267295
Web App URL: https://video-app-1111-dev.twil.io?passcode=xxxxxxxxxxxxxx
268296
Passcode: xxx xxx xxxx xxxx
297+
Room Type: group
269298
```
270299

271300
<!-- commandsstop -->

package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@twilio-labs/plugin-rtc",
3-
"version": "0.2.1",
3+
"version": "0.3.0",
44
"description": "A Twilio-CLI plugin for real-time communication apps",
55
"main": "index.js",
66
"publishConfig": {
@@ -22,6 +22,7 @@
2222
"@oclif/plugin-help": "^2",
2323
"@twilio-labs/serverless-api": "^4.0.1",
2424
"@twilio/cli-core": "^5.8.0",
25+
"lodash": "^4.17.20",
2526
"moment": "^2.24.0",
2627
"nanoid": "^3.1.12"
2728
},
@@ -35,7 +36,8 @@
3536
"jest": "^25.1.0",
3637
"lint-staged": "^10.0.8",
3738
"prettier": "^1.19.1",
38-
"superagent": "^5.2.2"
39+
"superagent": "^5.2.2",
40+
"twilio": "^3.48.2"
3941
},
4042
"eslintConfig": {
4143
"extends": "eslint:recommended",

src/commands/rtc/apps/video/deploy.js

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ DeployCommand.flags = Object.assign(
4242
default: false,
4343
description: 'Override an existing App deployment',
4444
}),
45+
'room-type': flags.enum({
46+
options: ['group', 'group-small', 'peer-to-peer'],
47+
description: 'Type of room to use',
48+
required: false,
49+
default: 'group',
50+
}),
4551
},
4652
TwilioClientCommand.flags
4753
);
@@ -68,22 +74,34 @@ DeployCommand.examples = [
6874
`# Deploy an application token server with passcode authentication
6975
$ twilio rtc:apps:video:deploy --authentication passcode
7076
deploying app... done
71-
Passcode: 1111111111`,
77+
Passcode: xxx xxx xxxx xxxx
78+
Expires: Mon Mar 09 2020 16:36:23 GMT-0600
79+
Room Type: group`,
7280
`
7381
# Deploy an application token server with the React app
7482
$ twilio rtc:apps:video:deploy --authentication passcode --app-directory /path/to/app
7583
deploying app... done
76-
Web App URL: https://video-app-1111-dev.twil.io?passcode=1111111111
77-
Passcode: 1111111111`,
84+
Web App URL: https://video-app-xxxx-xxxx-dev.twil.io?passcode=xxxxxxxxxxxxxx
85+
Passcode: xxx xxx xxxx xxxx
86+
Expires: Mon Mar 09 2020 16:36:23 GMT-0600
87+
Room Type: group`,
7888
`
7989
# Override an existing app with a fresh deployment
8090
# Please note that this will remove a previously deployed web application if no
8191
# app directory is provided
8292
$ twilio rtc:apps:video:deploy --authentication passcode --override
83-
Removed app with Passcode: 1111111111
93+
Removed app with Passcode: xxx xxx xxxx xxxx
94+
deploying app... done
95+
Passcode: yyy yyy yyyy yyyy
96+
Expires: Mon Mar 09 2020 16:36:23 GMT-0600
97+
Room Type: group`,
98+
`
99+
# Deploy an application token server with a specific room type
100+
$ twilio rtc:apps:video:deploy --authentication passcode --room-type peer-to-peer
84101
deploying app... done
85-
Passcode: 2222222222
86-
Expires: Mon Mar 09 2020 16:36:23 GMT-0600`,
102+
Passcode: xxx xxx xxxx xxxx
103+
Expires: Mon Mar 09 2020 16:36:23 GMT-0600
104+
Room Type: peer-to-peer`,
87105
];
88106

89107
module.exports = DeployCommand;

src/helpers.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,11 @@ async function getAppInfo() {
8181

8282
const passcodeVar = variables.find(v => v.key === 'API_PASSCODE');
8383
const expiryVar = variables.find(v => v.key === 'API_PASSCODE_EXPIRY');
84+
const roomTypeVar = variables.find(v => v.key === 'ROOM_TYPE');
8485

8586
const passcode = passcodeVar ? passcodeVar.value : '';
8687
const expiry = expiryVar ? expiryVar.value : '';
88+
const roomType = roomTypeVar ? roomTypeVar.value : '';
8789

8890
const fullPasscode = getPasscode(environment.domainName, passcode);
8991

@@ -93,6 +95,7 @@ async function getAppInfo() {
9395
sid: app.sid,
9496
passcode: fullPasscode,
9597
hasAssets: Boolean(assets.length),
98+
roomType,
9699
};
97100
}
98101

@@ -107,8 +110,13 @@ async function displayAppInfo() {
107110
if (appInfo.hasAssets) {
108111
console.log(`Web App URL: ${appInfo.url}`);
109112
}
113+
110114
console.log(`Passcode: ${appInfo.passcode.replace(/(\d{3})(\d{3})(\d{4})(\d{4})/, '$1 $2 $3 $4')}`);
111115
console.log(`Expires: ${appInfo.expiry}`);
116+
117+
if (appInfo.roomType) {
118+
console.log(`Room Type: ${appInfo.roomType}`);
119+
}
112120
}
113121

114122
async function deploy() {
@@ -151,6 +159,7 @@ TWILIO_API_SECRET = the secret for the API Key`);
151159
TWILIO_API_KEY_SECRET: this.twilioClient.password,
152160
API_PASSCODE: pin,
153161
API_PASSCODE_EXPIRY: expiryTime,
162+
ROOM_TYPE: this.flags['room-type'],
154163
},
155164
pkgJson: {},
156165
functionsEnv: 'dev',

src/video-token-server.js

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,34 @@ const AccessToken = Twilio.jwt.AccessToken;
55
const VideoGrant = AccessToken.VideoGrant;
66
const MAX_ALLOWED_SESSION_DURATION = 14400;
77

8-
module.exports.handler = (context, event, callback) => {
8+
module.exports.handler = async (context, event, callback) => {
99
const {
1010
ACCOUNT_SID,
1111
TWILIO_API_KEY_SID,
1212
TWILIO_API_KEY_SECRET,
1313
API_PASSCODE,
1414
API_PASSCODE_EXPIRY,
1515
DOMAIN_NAME,
16+
ROOM_TYPE,
1617
} = context;
1718

18-
const { user_identity, room_name, passcode } = event;
19+
const { user_identity, room_name, passcode, create_room = true } = event;
1920
const [, appID, serverlessID] = DOMAIN_NAME.match(/-?(\d*)-(\d+)(?:-\w+)?.twil.io$/);
2021

2122
let response = new Twilio.Response();
2223
response.appendHeader('Content-Type', 'application/json');
2324

25+
if (typeof create_room !== 'boolean') {
26+
response.setStatusCode(400);
27+
response.setBody({
28+
error: {
29+
message: 'invalid parameter',
30+
explanation: 'A boolean value must be provided for the create_room parameter',
31+
},
32+
});
33+
return callback(null, response);
34+
}
35+
2436
if (Date.now() > API_PASSCODE_EXPIRY) {
2537
response.setStatusCode(401);
2638
response.setBody({
@@ -30,8 +42,7 @@ module.exports.handler = (context, event, callback) => {
3042
'The passcode used to validate application users has expired. Re-deploy the application to refresh the passcode.',
3143
},
3244
});
33-
callback(null, response);
34-
return;
45+
return callback(null, response);
3546
}
3647

3748
if (API_PASSCODE + appID + serverlessID !== passcode) {
@@ -42,8 +53,7 @@ module.exports.handler = (context, event, callback) => {
4253
explanation: 'The passcode used to validate application users is incorrect.',
4354
},
4455
});
45-
callback(null, response);
46-
return;
56+
return callback(null, response);
4757
}
4858

4959
if (!user_identity) {
@@ -54,8 +64,27 @@ module.exports.handler = (context, event, callback) => {
5464
explanation: 'The user_identity parameter is missing.',
5565
},
5666
});
57-
callback(null, response);
58-
return;
67+
return callback(null, response);
68+
}
69+
70+
if (create_room) {
71+
const client = context.getTwilioClient();
72+
73+
try {
74+
await client.video.rooms.create({ uniqueName: room_name, type: ROOM_TYPE });
75+
} catch (e) {
76+
// Ignore 53113 error (room already exists). See: https://www.twilio.com/docs/api/errors/53113
77+
if (e.code !== 53113) {
78+
response.setStatusCode(500);
79+
response.setBody({
80+
error: {
81+
message: 'error creating room',
82+
explanation: 'Something went wrong when creating a room.',
83+
},
84+
});
85+
return callback(null, response);
86+
}
87+
}
5988
}
6089

6190
const token = new AccessToken(ACCOUNT_SID, TWILIO_API_KEY_SID, TWILIO_API_KEY_SECRET, {
@@ -65,6 +94,6 @@ module.exports.handler = (context, event, callback) => {
6594
const videoGrant = new VideoGrant({ room: room_name });
6695
token.addGrant(videoGrant);
6796
response.setStatusCode(200);
68-
response.setBody({ token: token.toJwt() });
69-
callback(null, response);
97+
response.setBody({ token: token.toJwt(), room_type: create_room ? ROOM_TYPE : null });
98+
return callback(null, response);
7099
};

0 commit comments

Comments
 (0)