module ORTC

The ORTC (Open Real-Time Connectivity) was developed to add a layer of abstraction to real-time full-duplex web communications platforms by making real-time web applications independent of those platforms.

ORTC provides a standard software API (Application Programming Interface) for sending and receiving data in real-time over the web.

ORTC-API requires the following packages to be installed:

Constants

MAX_CHANNEL_NAME_SIZE
MAX_CONNECTION_METADATA_SIZE
MAX_HEARTBEAT_INTERVAL
MAX_MESSAGE_SIZE
RECONNECT_INTERVAL

Public Class Methods

_get_cluster(url) click to toggle source
# File ortc.rb, line 242
def self._get_cluster(url)
  begin                               
    uri = URI.parse(url)
    if http = Net::HTTP.new(uri.host, uri.port)
      if url.include? 'https'
        http.use_ssl = true
        http.verify_mode = OpenSSL::SSL::VERIFY_NONE
      end
      uri = URI.parse(url<< "?appkey=#{@app_key}")   
      http.start do |http|
        request = Net::HTTP::Get.new(uri.request_uri)
        response = http.request(request)
        response.body.scan(%r"(.*?)"/).join
      end
    end
  rescue Timeout::ExitException
    return ''
  rescue Timeout::Error
    return ''
  rescue => e
    return ''
  end
end
disable_presence(url, is_cluster, application_key, private_key, channel, &block) click to toggle source

Disables presence for the specified channel.

Note: This method will send your Private Key over the Internet. Make sure to use secure connection.

  • url - Server containing the presence service

  • is_cluster - Indicates whether the url is in a cluster.

  • application_key - Application key with access to presence service

  • private_key - The private key provided when the ORTC service is purchased.

  • channel - Channel to disable presence.

  • &block - Callback with error and result parameters.

Usage:

ORTC.disable_presence(ortc_url, true, ortc_app_key, ortc_private_key, channel) { |error, result| 
    if error.to_s.empty?
        puts "result: #{result}"
    else
        puts "error: #{error}"
    end
}
# File ortc.rb, line 118
def self.disable_presence(url, is_cluster, application_key, private_key, channel, &block)
  if url.to_s.empty?
    block.call('URL is null or empty', nil)
  elsif application_key.to_s.empty?
    block.call('Application Key is null or empty', nil)
  elsif private_key.to_s.empty?
    block.call('Private key is null or empty', nil)
  elsif channel.to_s.empty?
    block.call('Channel is null or empty', nil)
  elsif not channel =~ %r^[\w\-:\/.]+$/
    block.call('Channel has invalid characters', nil)
  else
    begin
      r_thread = Thread.new { 
        server = ''
        if is_cluster
          server = _get_cluster(url)
          begin
            block.call('Can not connect with the server', nil)
            r_thread.exit
          end if server == ''
        else 
          server = url.clone
        end
        server = server << (server.match(%r\/$/) ? 'presence' : '/presence')
        server = server << "/disable/#{application_key}/#{channel}"
        body = "privatekey=#{private_key}"
        uri = URI.parse(server)
        begin
          if http = Net::HTTP.new(uri.host, uri.port)
            if server.match %r^https/
              http.use_ssl = true
              http.verify_mode = OpenSSL::SSL::VERIFY_NONE
            end
            req = Net::HTTP::Post.new(uri.request_uri)
            req.body = body
            res = http.request(req)
            if res.code == '200'
              block.call(nil, res.body)
            else
              block.call(res.body, nil)
            end
          end
        rescue => e
          block.call(e, nil)
          r_thread.exit
        end
      }
      r_thread.run
    end
  end
end
enable_presence(url, is_cluster, application_key, private_key, channel, metadata, &block) click to toggle source

Enables presence for the specified channel with first 100 unique metadata if true.

Note: This method will send your Private Key over the Internet. Make sure to use secure connection.

  • url - Server containing the presence service

  • is_cluster - Indicates whether the url is in a cluster.

  • application_key - Application key with access to presence service

  • private_key - The private key provided when the ORTC service is purchased.

  • channel - Channel to activate presence.

  • metadata - Defines if to collect first 100 unique metadata.

  • &block - Callback with error and result parameters.

Usage:

ORTC.enable_presence(ortc_url, true, ortc_app_key, ortc_private_key, channel, true) { |error, result| 
    if error.to_s.empty?
        puts "result: #{result}"
    else
        puts "error: #{error}"
    end
}
# File ortc.rb, line 47
def self.enable_presence(url, is_cluster, application_key, private_key, channel, metadata, &block)
  if url.to_s.empty?
    block.call('URL is null or empty', nil)
  elsif application_key.to_s.empty?
    block.call('Application Key is null or empty', nil)
  elsif private_key.to_s.empty?
    block.call('Private key is null or empty', nil)
  elsif channel.to_s.empty?
    block.call('Channel is null or empty', nil)
  elsif not channel =~ %r^[\w\-:\/.]+$/
    block.call('Channel has invalid characters', nil)
  else
    begin
      r_thread = Thread.new { 
        server = ''
        if is_cluster
          server = _get_cluster(url)
          begin
            block.call('Can not connect with the server', nil)
            r_thread.exit
          end if server == ''
        else 
          server = url.clone
        end
        server = server << (server.match(%r\/$/) ? 'presence' : '/presence')
        server = server << "/enable/#{application_key}/#{channel}"
        body = "privatekey=#{private_key}&metadata=" << (metadata ? '1' : '0')
        uri = URI.parse(server)
        begin
          if http = Net::HTTP.new(uri.host, uri.port)
            if server.match %r^https/
              http.use_ssl = true
              http.verify_mode = OpenSSL::SSL::VERIFY_NONE
            end
            req = Net::HTTP::Post.new(uri.request_uri)
            req.body = body
            res = http.request(req)
            if res.code == '200'
              block.call(nil, res.body)
            else
              block.call(res.body, nil)
            end
          end
        rescue => e
          block.call(e, nil)
          r_thread.exit
        end
      }
      r_thread.run
    end
  end
end
presence(url, is_cluster, application_key, authentication_token, channel, &block) click to toggle source

Gets a Hash table indicating the subscriptions in the specified channel and if active the first 100 unique metadata.

  • url - Server containing the presence service

  • is_cluster - Indicates whether the url is in a cluster.

  • application_key - Application key with access to presence service

  • authentication_token - Authentication token with access to presence service

  • channel - Channel to presence data active.

  • &block - Callback with error and result parameters.

Usage:

ORTC.presence(ortc_url, true, ortc_app_key, ortc_auth_token, channel) { |error, result| 
    if error.to_s.empty?
        puts "result: #{result}"
    else
        puts "error: #{error}"
    end
}
# File ortc.rb, line 187
def self.presence(url, is_cluster, application_key, authentication_token, channel, &block)
  if url.to_s.empty?
    block.call('URL is null or empty', nil)
  elsif application_key.to_s.empty?
    block.call('Application Key is null or empty', nil)
  elsif authentication_token.to_s.empty?
    block.call('Authentication Token is null or empty', nil)
  elsif channel.to_s.empty?
    block.call('Channel is null or empty', nil)
  elsif not channel =~ %r^[\w\-:\/.]+$/
    block.call('Channel has invalid characters', nil)
  else
    begin
      r_thread = Thread.new { 
        server = ''
        if is_cluster
          server = _get_cluster(url)
          begin
            block.call('Can not connect with the server', nil)
            r_thread.exit 
          end if server == ''
        else 
          server = url.clone
        end
        server = server << (server.match(%r\/$/) ? 'presence' : '/presence')
        server = server << "/#{application_key}/#{authentication_token}/#{channel}"
        #body = "privatekey=#{private_key}"
        uri = URI.parse(server)
        begin
          if http = Net::HTTP.new(uri.host, uri.port)
            if server.match %r^https/
              http.use_ssl = true
              http.verify_mode = OpenSSL::SSL::VERIFY_NONE
            end
            req = Net::HTTP::Get.new(uri.request_uri)                                                 
            res = http.request(req)
            if res.code == '200'
              ret = Hash.new
              ret = JSON.parse(res.body) if not res.body == 'null'
              block.call(nil, ret)
            else
              block.call(res.body, nil)
            end
          end
        rescue => e
          block.call(e, nil)
          r_thread.exit
        end
      }
      r_thread.run
    end
  end
end