Using twilio video libraries in app suggestions on token server setup?
Hi, So I have been working with a project where I am integrating twilio video chat into an existing app which uses firebase for the authorization/database using Swift 4. Even following along with twill's own written tutorial I have hit a wall. I have an app I am working with using swift 4 for iOS development. I have added the twilio framework for video chat and firebase for user authorization and database functionality. I am able to setup a test chat between 2 users but, where I am having an issue is automated process of setting up a room, retrieving a token from my setup server, and sending an invite to a user to join the conference. To be more specific the app is setup to use Firebase user authentication, when a user wants to join a video chat they click on a button and are directed to a view controller where "friends" of the user are listed, when a user selects a friend they click a button to start a chat, the app creates an entry in firebase to create a database entry for the chat which creates a numeric hash value of the usernames to generate a name for the chat room in the back end and forwards the user to the video chat controller. Where I am stuck is how/what to write in swift code to send the room name to the token server, retrieve the token, send the token to the original users video controller, send an in app alert to the invited user, with a clickable link the invited user joins the conference, and create an entry in the database once the conference is started. Does anyone have a more detailed tutorial they can link me to so I can figure this out or maybe a completed app on Git (not twilios) so maybe I can see what I am missing? Thanks in advance.
Posted Jun 8, 2018, 12:09 AM UTC
Twilio requires a dedicated backend server that uses one of their client libraries. The idea is to protect your access key which could otherwise be easily extracted from the mobile app. You can't get the token directly from the app.
They do not provide direct REST API access...
I have asked them if there is any way to access the API from a Swift backend (e.g. Vapor), but no, it's not, so your best bet is to host a bare minimum backend in one of the supported languages and use it to get the token from Twilio and pass it to the iOS app.
The node code to get a token and return it to the app is like 10 lines. You can host that on Heroku for free.
Got it. And I setup the twilio code script on a lightsail instance which word very well. I guess my question is on the iOS side. I am failing to see what code block from the app sends the room and user information and what specific code block receives it to generate a room.
I have a network abstraction layer, where I have a method that calls my backend (Parse in this instance) and provides the room id (I generate a new UUID for each call) and user identityt (the objectId of my PFUser).
The Parse cloud function then calls the Twilio API via their Node library, provides the room id and user identity and passes the Access Token returned byTwilio back to the app.
I also have a Twilio abstraction layer in my app, where I ask Twilio to connect to a room with the room id I have used for the access token and the access token that I got back from Twilio via Parse.
I also send push notifications to the users being called via Parse.
In terms of code, here is the code for requesting a token via Parse:
Here is the Parse code that gets the token from Twilio
And this is the Twilio code to connect to the room
Thank you so much this extremely helpful!
One last question I am trying to understand how to assign the users to there own cell in a UICollectionView, have you run across a tutorial or sample code for that?