Skip to content

Instantly share code, notes, and snippets.

@jonmumm
Created July 17, 2012 22:30
Show Gist options
  • Save jonmumm/3132583 to your computer and use it in GitHub Desktop.
Save jonmumm/3132583 to your computer and use it in GitHub Desktop.
OpenTok Heroku Readme

OpenTok is a hosted API for adding real-time video chat to your application. The API gives developers the ability to create private one-to-one chats, group chats, or broadcasts that can stream to thousands of viewers in real-time.

When developing with OpenTok, most of the development is done on the client. The server is used only to generate and distribute room ids (sessions) and security credentials (tokens) to the client.

OpenTok has client-side libraries for JavaScript, iOS, and ActionScript, and server-side libraries for Ruby, PHP, Java, Python, .NET and Node.js. This add-on will set up your server-side environment for use with OpenTok.

Provisioning the add-on

OpenTok can be attached to a Heroku application via the CLI:

A list of all plans available can be found [here](http://addons.heroku.com/opentok).
:::term
$ heroku addons:add opentok
-----> Adding opentok to sharp-mountain-4005... done, v18 (free)

Adding OpenTok will add three variables to your application config: OPENTOK_API_KEY, OPENTOK_API_SECRET, OPENTOK_API_URL. This can be confirmed using the heroku config command.

:::term
$ heroku config | grep OPENTOK
OPENTOK_API_KEY    => "YOUR API KEY"
OPENTOK_API_SECRET => "YOUR API SECRET"
OPENTOK_API_URL    => http://api.opentok.com/hl

After installing OpenTok the application should be configured to fully integrate with the add-on.

Local setup

Environment setup

After provisioning the add-on it's necessary to locally replicate the config vars so your development environment can operate against the service.

Using Foreman

We recommend using Foreman to manage your environment variables. Use the following command to add the OPENTOK values retrieved from heroku config to .env.

:::term
$ heroku config -s | grep OPENTOK >> .env

Other

If you are not using Foreman, you must add the OPENTOK variables to your environment in some other way. Here is a script that will add the variables to your .bash_profile and reload your environment:

:::term
$ heroku config -s | grep OPENTOK | sed "s/\(.*\)/export \1/g" >> ~/.bash_profile
$ source ~/.bash_profile

Using with Rails 3.x

Ruby on Rails applications will need to add the following entry into their Gemfile specifying the OpenTok client library.

:::ruby
gem 'opentok'

Update application dependencies with bundler.

:::term
$ bundle install
sample output

With the OpenTok gem installed, create an initializer to create an instance of the OpenTok SDK accessible across your application.

config/initializers/opentok.rb

:::ruby
# Initialize OpenTok
::OPENTOK = OpenTok::OpenTokSDK.new(
  ENV["OPENTOK_API_KEY"], 
  ENV["OPENTOK_API_SECRET"], 
  :api_url => ENV["OPENTOK_API_URL"]
)

Creating Sessions

OpenTok revolves around the concept of sessions. Think of a session as a room. You create sessions on your server and then distribute them to your JavaScript or iOS clients that you would like in the same room.

Create a session with the following code:

:::ruby
@session = OPENTOK.create_session

Sessions do not expire and can be stored in your database.

Generating Tokens

To connect to a session, each user needs a token. Think of a token as a key to get in to a room. Tokens are what provide security. You can use them to specify what permissions a user has.

Generate a token with the following code:

:::ruby
@token = OPENTOK.generate_token :session_id => @session.to_s

Tokens have a default expiration time of 15 minutes and should not be stored in your database. A new token should be generated each time a user connects to a session.

Writing the Client

In most OpenTok application, sessions and tokens are generated in the controller, and then passed down to the view for the client to connect to. You can use either JavaScript, iOS, or ActionScript.

Here are a few resources to get started developing the client:

Removing the add-on

OpenTok can be removed via the CLI.

:::term
$ heroku addons:remove opentok
-----> Removing opentok from sharp-mountain-4005... done, v20 (free)

Support

All OpenTok support and runtime issues should be submitted via the Heroku Support channels. Any non-support related issues or product feedback is welcome at OpenTok Support.

Additional resources

Additional resources are available at:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment