Session Creation — Ruby
You can use the OpenTok Ruby library to generate OpenTok sessions. Each session has a unique session ID, which you can use in an OpenTok client library to connect to an OpenTok session. (See "Joining a Session" for Web, iOS, or Android.)
Creating a session that uses the OpenTok Media Router
The following code creates a session that uses the OpenTok Media Router:
api_key = "your_API_key" # Replace with your OpenTok API key.
api_secret = "your_API_secret" # Replace with your OpenTok API secret.
opentok = OpenTok::OpenTok.new api_key, api_secret
session = opentok.create_session :media_mode => :routed
session_id = session.session_id
Use the session ID in an OpenTok client library to connect to an OpenTok session.
You will also need to generate a token for each user connecting to the OpenTok session. See Connection Token Creation for information on the generate_token() method.
The OpenTok Media Router provides the following benefits:
- The OpenTok Media Router can decrease bandwidth usage in multiparty sessions. (In a relayed session, each client must send a separate audio-video stream to each client subscribing to it.)
- The OpenTok Media Router can improve the quality of the user experience through audio fallback and video recovery. With these features, if a client's connectivity degrades to a degree that it does not support video for a stream it's subscribing to, the video is dropped on that client (without affecting other clients), and the client receives audio only. If the client's connectivity improves, the video returns.
- The OpenTok Media Router supports the archiving feature, which lets you record, save, and retrieve OpenTok sessions.
Creating a relayed session
Here is Ruby sample code that creates a new session with the media mode set to relayed:
api_key = "your_API_key" # Replace with your OpenTok API key.
api_secret = "your_API_secret" # Replace with your OpenTok API secret.
opentok = OpenTok::OpenTok.new api_key, api_secret
session = opentok.create_session
session_id = session.session_id
In a relayed session, clients will attempt to send streams directly between each other. However, if clients cannot connect due to firewall restrictions, the session uses the OpenTok TURN server to relay audio-video streams.
Important: Some features, such as archiving, are only available in routed (not relayed) sessions. See the previous section for instructions on creating a routed session.
Creating an automatically archived session
You can create a session that is automatically archived. Here is Ruby sample code that creates an automatically archived session:
api_key = "your_API_key" # Replace with your OpenTok API key.
api_secret = "your_API_secret" # Replace with your OpenTok API secret.
opentok = OpenTok::OpenTok.new api_key, api_secret
session = opentok.create_session :archive_mode => :always, :media_mode => :routed
session_id = session.session_id
Note that archived sessions must use the routed media mode.
For more information, see the archiving developer guide.
Using sessions in client applications
Use the session ID in an OpenTok client SDK to connect to an OpenTok session.
You will also need to generate a token for each user connecting to the OpenTok session. See Connection Token Creation for information on the generate_token() method.