How to build Live-Streaming Service with Ruby on Rails and TokBox
Live streaming services development is one of the fast-growing trends in current mainstream development. There are numerous examples of the ways live videos can be used in. There is an online video chat for small groups, live Yoga classes, an online doctor appointment service, live gaming, etc. Have you ever been curious about the difficulty of building a live-streaming service? If you’ve had, we’ll surprise you: it can be done just in an hour! Let us suppose that you are not going to build everything from scratch, and you’ll just connect some already existing technologies with each other to create something new instead. First of all, you will have to sign-up on https://tokbox.com/ and get your API key, and a secret token for further usage in the Rails app. This process is pretty straightforward and you’ll find all necessary keys right on the dashboard as soon as the registration is completed. That’s it!
Step 1. Create new app and add some configuration
Let’s begin with the creation of a new Rails app. For this purpose we recommend you to use Rails 4:
$ rails new live-streaming
Once it’s done, let’s add some gems to our Gemfile:
gem 'figaro' # we'll use it to store our API keys gem 'gon' # this one to expose our API keys to JS gem 'opentok' # SDK to use TokBox library gem 'devise' # I bet you know whats is it :)
We’ll use gon gem to expose session ID of the stream to the browser as JS variable. To be able to access JS variables, make sure to add the following line into to the application layout file:
<%= gon::base.render_data="" %=""></%=>
We’ll use figaro gem to expose our secret API keys to Rails.
$ bundle exec figaro install
It will create config/application.yml where we’ll store our API keys to access the OpenTOK.
You should put your API keys there which you’ve got from the TokBox website:
opentok_api_key: paste_your_api_key opentok_api_secret: paste_your_secret_key
Now, these variables will be accessible from our Rails app using Figaro.env object:
Pretty handy, isn’t it?
Step 2. Add OpenTok client
To connect to the OpenTOK platform and use their AP, we have to add a client.
To achieve that, let’s create an initializer file in /config/initializers/: we’ve added a simple constant for the OpenTok client, which allows us to access it anywhere in the Rails app.
Now we have to make sure that it works properly.
Open the rails console and try to create a new session for OpenTok:
$ rails c >OpenTokClient.create_session
You’ll see the dump of a newly created OpenTOK session.
Step 3. Generate some models
First, let’s install the Devise and create a user model with the help of the device generator.
$ rails g devise:install $ rails g devise user
After that, we’ll generate stream resources able to create a model, migration, a controller, and some scaffolds for scripts and tests.
$ rails g resource stream user:references desc:text status:string tokbox_session_id:string
We’ll have to set the routes root to the point to our stream:
We’ll also need to add simple references to our models and a callback to generate a new session ID for each new stream.
This is how the models will look at this stage:
Then run migrations and we are set for now.
$ rake db:migrate
Step 4. A little of coffee script and html
This part is pretty simple,too, so we’ll just need two different actions: to watch and to stream the video. It means that we have to create:
- two controller actions
- two views for these actions
- two JS files for these views
- new routes
At first, we have to generate a new token each time anew when we are going to stream or watch the video. OpenTok SDK provides a method called generate_token which requires session_ID as first arguments and also accepts options hash to set additional data and capabilities for the token.
Read more here on the token creation process: https://www.tokbox.com/developer/guides/create-token/
Step 5. Run the app!
Finally, it’s high time to run our own stream!
Run rails server
$ rails s -p 3000
You are welcome to visit your app at http://localhost:3000, so after a quick signup process, you’ll be able to start your own stream and watch it.
Step 6. How to deploy the app on Heroku
If you want to make your app open to the public and let other people get connected and watch the streams, you can deploy it to Heroku in a few minutes.
- $ git clone [email protected]:BitOfUniverse/broadcaster.git
- $ cd broadcaster
- add your OpenTok api keys to config/application.yml
- $ heroku create
- $ figaro heroku:set -e production
- $ git push heroku master
- $ heroku open
That’s it! Now you have made sure that building a live-streaming service really is a quick and time-saving process.
Source code: https://github.com/BitOfUniverse/broadcaster
Read more on OpenTok API usage:
If you have any questions, email us [email protected]