This is a simple demo app that shows how you can use the OpenTok-Ruby-SDK to create Sessions, generate Tokens with those Sessions, and then pass these values to a JavaScript client that can connect and conduct a group chat.
First, download the dependencies using Bundler
$ bundle install
Next, add your own API Key and API Secret to the environment variables. There are a few ways to do this but the simplest would be to do it right in your shell.
$ export API_KEY=0000000
$ export API_SECRET=abcdef1234567890abcdef01234567890abcdef
Finally, start the server using Bundler to handle dependencies
$ bundle exec ruby hello_world.rb
Visit http://localhost:4567 in your browser. Open it again in a second window. Smile! You've just set up a group chat.
This demo application uses the Sinatra web framework. It is similar to many other popular web frameworks, such as Rails, but more light-weight. We are only covering the very basics of the framework, but you can learn more by following the links above.
The first thing done in this file is to require the dependencies we will be using. In this case that
is the Sinatra web framework and most importantly the OpenTok SDK. By running the application with
the bundle exec command, we allow Bundler to select the right versions of these gems from the
right place.
require 'sinatra/base'
require 'opentok'Next this file performs some basic checks on the environment. If it cannot find the API_KEYand
API_SECRET environment variables, there is no point in continuing.
The class HelloWorld is our application. The first thing it does is to initialize an instance of
OpenTok and also store it using Sinatra's set method so it can be accessed in other parts of the
application. At the same time, we also set the api_key separately so that we can access
it on its own.
set :api_key, ENV['API_KEY']
set :opentok, OpenTok::OpenTok.new(api_key, ENV['API_SECRET'])Now, lets discuss the Hello World application's functionality. We want to set up a group chat so
that any client that visits a page will connect to the same OpenTok Session. Once they are connected
they can Publish a Stream and Subscribe to all the other streams in that Session. So we just need
one Session object, and it needs to be accessible every time a request is made. The next line of our
application simply calls the OpenTok instance's create_session method and once again uses set
to store it. Alternatively, session_ids are commonly stored in databses for applications that have
many of them.
set :session, opentok.create_sessionWe only need one page, so we create one route handler for any HTTP GET requests to trigger.
get '\' do
# ...
endNow all we have to do is serve a page with the three values the client will need to connect to the
session: api_key, session_id, and token. The first two are right on the settings object
because we called set earlier. Note that the we call the session_id method to get just that
value from the session object. The token is generated freshly on this request by calling the
generate_token method of the opentok instance, and passing in the session_id. This is because
a Token is a piece of information that carries a specific client's permissions in a certain Session.
Ideally, as we've done here, you generate a unique token for each client that will connect.
api_key = settings.api_key
session_id = settings.session.session_id
token = settings.opentok.generate_token(session_id)Now all we have to do is serve a page with those three values. Lets call our erb helper that will
pick up a template called index.erb from the views/ directory in our application and pass in
the local variables for it to print using the :locals hash.
erb :index, :locals => {
:api_key => api_key,
:session_id => session_id,
:token => token
}This file simply sets up the HTML page for the JavaScript application to run, imports the
JavaScript library, and passes the values created by the server into the JavaScript application
inside public/js/helloworld.js
The group chat is mostly implemented in this file. At a high level, we connect to the given Session, publish a stream from our webcam, and listen for new streams from other clients to subscribe to.
For more details, read the comments in the file or go to the JavaScript Client Library for a full reference.