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:
<pre>$ rails new live-streaming</pre>
Once it’s done, let’s add some gems to our Gemfile:
<pre>gem 'figaro' # we'll use it to store our API keys</pre>
<pre>gem 'gon' # this one to expose our API keys to JS</pre>
<pre>gem 'opentok' # SDK to use TokBox library</pre>
<pre>gem 'devise' # I bet you know whats is it :)</pre>
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 folowing line into to the application layout file:
<pre><%= gon::base.render_data="" %=""></%=></pre>
We’ll use figaro gem to expose our secret API keys to the Rails.
<pre>$ bundle exec figaro install</pre>
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:
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, whicht 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:
<pre>$ rails c</pre>
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.
<pre>$ rails g devise:install</pre>
<pre>$ rails g devise user</pre>
After that, we’ll generate stream resources able to create a model, migration, a controller and some scaffolds for scripts and tests.
<pre>$ rails g resource stream user:references desc:text status:string tokbox_session_id:string</pre>
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 the each new stream.
This is how the models will look at this stage:
Then run migrations and we are set for now.
<pre>$ rake db:migrate</pre>
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
<pre>$ rails s -p 3000</pre>
You are welcome to visit your app on 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 you app open to public and let other people get connected and watch the streams, you can deploy it to Heroku in few minutes.
- $ git clone firstname.lastname@example.org: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: