top of page
Forfatterens bildeVaibhav Dhoke

Create VC using Daily.co & Rails




Rails is a popular web application framework for building robust and scalable web applications. Daily.co is a platform for building real-time video and audio applications, including video conferencing. In this blog, we will walk through the steps to create a video conferencing system using Rails and Daily.co.


Step 1: Create a new Rails application

The first step is to create a new Rails application using the command line tool. Open the terminal and run the following command:




rails new videoconferencing --database=postgresql

This command creates a new Rails application named "videoconferencing" with PostgreSQL as the default database.


Step 2: Add HTTP Party SDK

Next, add the HTTP Party Gem to the application to make api calls to daily to create room. Open the `Gemfile` in the root directory of the application and add the following line:



gem 'httparty'

Save the file and run `bundle install` in the terminal to install the gem.


Step 3: Configure Daily.co API credentials

To use the credentials file to store api_key. Use this command to add `EDITOR=nano rails credentials:edit` :



daily:
   access_token: YOUR_API_KEY

Replace `YOUR_API_KEY` with your Daily.co API Key which you can get from daily developers section `https://dashboard.daily.co/developers` .


Step 4: Create daily api service for creating video conference room

To create a new video conference room, create a folder named services in app folder, inside `services` create another folder `dailyco`, inside `dailyco` create a file named `room.rb` and add the following code in room.rb :




class Dailyco::Room
  include HTTParty
  base_uri 'https://api.daily.co/v1'
  
  def initialize(name)
    @name = name
  end

  def create
    options = auth_headers
    body = {name: @name}
    self.class.post('/rooms/', options.merge({body: body.to_json}))
  end

  def auth_headers
    access_token = Rails.application.credentials.config[:daily][:access_token]
    {
      headers: {
        'Content-Type' => 'application/json',
        'Authorization' => "Bearer #{access_token}",
      }
    }
  end
end



Step 5: Create a new video conference room

To create a new video conference room, create a new controller named `video_conferences_controller.rb` in the `app/controllers` directory or you can use command `rails g controller VideoConferences` and add the following code :



class VideoConferencesController < ApplicationController
  def create
    daily = Dailyco::Room.new(params[:name])
    room = JSON.parse daily.create
    redirect_to room['url']
  end
end

This code creates a new video conference room with the name provided in the `params` hash and redirects the user to the room URL.


Step 6: Create a view to display the video conference form

Create a new view named `new.html.erb` in the `app/views/video_conferences` directory and add the following code:



<h1>Create a new video conference room</h1>
<%= form_tag video_conferences_path, method: :post do %>
  <%= label_tag :name, 'Room Name' %>
  <%= text_field_tag :name %>
  <%= submit_tag 'Create Room' %>
<% end %>

This code displays a form to create a new video conference room and submit it to the `create` action of the `VideoConferencesController`.


Step 6: Start the Rails server

Start the Rails server using the following command, if you face database not found error you can create db using command `rake db:create`:



rails server

Open the browser and navigate to `http://localhost:3000/video_conferences/new`. This should display the video conference form. Enter a name for the video conference room and click on the "Create Room" button.


Step 7: Join the video conference

To join the video conference, click on the link provided on the next page. This should open the video conference in a new tab. You can share this link with other participants to join the video conference.


In conclusion, Daily.co provides a powerful API for building real-time video and audio applications, including video conferencing. By integrating into a Rails application, you can easily create a video conferencing system that is scalable and customizable. With the steps outlined in this blog, you can create a simple video conferencing system using Rails and Daily.co in no time.

24 visninger0 kommentarer

Siste innlegg

Se alle

Comments


bottom of page