Skip to content

Instantly share code, notes, and snippets.

@yunjian
Created February 28, 2011 21:13
Show Gist options
  • Save yunjian/848051 to your computer and use it in GitHub Desktop.
Save yunjian/848051 to your computer and use it in GitHub Desktop.
Use Opentok API in Rails
# 1. plugin
# Download the ruby SDK code, and copy to the plugin directory.
${RAILS_ROOT}/vendor/plugins/opentok
# You may need to create an "init.rb" file in the opentok plugin directory,
# and insert the following line.
require 'opentok'
# 2. database
# I have a set of "topics", where each topic page contains an opentok video session.
# So, I created a database table to store the opentok session ID for each topic.
# Below is the code for db migration.
class CreateOpentokSessions < ActiveRecord::Migration
def self.up
create_table :opentok_sessions do |t|
t.integer :topic_id
t.string :session_id, :default => "", :null => false
end
add_index :opentok_sessions, :topic_id
end
def self.down
drop_table :opentok_sessions
end
end
# 3. model
# Create the corresponding model for opentok session and the relationship with topics.
# opentok_session.rb
class OpentokSession < ActiveRecord::Base
belongs_to :topic, :class_name => 'Topic'
end
# topic.rb
class Topic < ActiveRecord::Base
...
has_one :opentok_session, :class_name => "OpentokSession", :foreign_key => "topic_id", :dependent => :destroy
...
end
# 4. controller
# In the topics controller, retrieve the opentok session ID or create a new one
# if it does not exist yet. Then pass the session ID and generated token to the view.
# function to initialize the opentok object
def config_opentok
if @opentok.nil?
@opentok = OpenTok::OpenTokSDK.new YOUR_OPENTOK_API_KEY, "YOUR_OPENTOK_API_SECRET"
@opentok.api_url = 'https://staging.tokbox.com/hl'
end
end
def show
...
config_opentok
tok_session = OpentokSession.find(:first, :conditions => {:topic_id => @topic})
if tok_session.nil?
id = @opentok.create_session '127.0.0.1'
tok_session = OpentokSession.create(:topic_id => @topic.id, :session_id => id.to_s)
tok_session.save
end
@tok_session_id = tok_session.session_id
@tok_token = @opentok.generate_token :session_id => @tok_session_id
...
end
# 5. view
#
# In the topic view, insert the javascript and show the opentok video panel.
# This is basically the same as in the opentok tutorial.
<script type="text/javascript" charset="utf-8">
var apiKey = YOUR_OPENTOK_API_KEY;
var sessionId = "<%= @tok_session_id %>";
var token = "<%= @tok_token %>";
</script>
<script src="http://192.168.1.4:3000/javascripts/tokbox.js" type="text/javascript"></script>
<div class="tokbox_frame" id="tokbox_frame">
<div id="links">
<a href="#" onclick="javascript:connect();" id="connectLink">Join video</a>
<a href="#" onclick="javascript:publish();" id="publishLink">Publish video</a>
<a href="#" onclick="javascript:unpublish();" id="unpublishLink">Unpublish video</a>
</div>
<div id="publisherDiv" class="publisherContainer"></div>
<div id="subscriberBar"></div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment