Friday, 17 March 2017

WebRTC Opentok in Ruby On Rails for Text chat


This post will take you through adding text chat functionality using Opentok (a WebRTC platform) in Ruby on Rails. OpenTok is a WebRTC platform for embedding live video, voice, and messaging into your websites and mobile apps. Opentok has its own signalling server. We will be using the OpenTok signaling API.
First, we will make the basic setup for using Opentok service. Here we will be using ‘opentok’ ruby gem.

1) Add opentok gem to your Gemfile.

gem ‘opentok’

2) Then create a session that will attempt to transmit streams directly between clients.

Store session id and token somewhere in the database, so that you can use it later on.
Here token gets expired in 30 days(default value). You will need to regenerate after 30 days.

3) Now it’s time to connect to session that we have created above, Add this to your HTML page,

A signal is sent using the signal() method of the Session object. One can receive a signal by listening to a signal event dispatched by session object.
So, All clients who are connected to the same session and listening to same signal type(‘text_chat’ here) will receive a message as soon as someone publishes to channel ‘text_chat’.

2 comments:

  1. This is a great writeup - thanks for covering OpenTok's Signaling API.

    One quick note, since you're using an .erb - instead of hardcoding the same session ID and session token into the HTML, you can instead generate a new session ID and session token in your route handler for this page, and dynamically place that into the HTML.

    This is actually the best practice - to use a new session ID and token each time.

    (disclaimer: I work for Tokbox :D)

    ReplyDelete
    Replies
    1. Thanks for pointing out that! Looks like I did that by mistake. Updated the post now.

      Delete