Project

pubcontrol

0.0
Low commit activity in last 3 years
A long-lived project that still receives updates
A Ruby convenience library for publishing messages using the EPCP protocol
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

 Project Readme

ruby-pubcontrol

Author: Konstantin Bokarius kon@fanout.io

A Ruby convenience library for publishing messages using the EPCP protocol.

License

ruby-pubcontrol is offered under the MIT license. See the LICENSE file.

Installation

gem install pubcontrol

Usage

require 'pubcontrol'

class HttpResponseFormat < Format
  def initialize(body)
    @body = body
  end

  def name
    return 'http-response'
  end

  def export
    return {'body' => @body}
  end
end

def callback(result, message)
  if result
    puts 'Publish successful'
  else
    puts 'Publish failed with message: ' + message.to_s
  end
end

# PubControl can be initialized with or without an endpoint configuration.
# Each endpoint can include optional JWT authentication info.
# Multiple endpoints can be included in a single configuration.

# Initialize PubControl with a single endpoint:
pub = PubControl.new({'uri' => 'https://api.fanout.io/realm/<myrealm>',
    'iss' => '<myrealm>', 'key' => Base64.decode64('<realmkey>')})

# Add new endpoints by applying an endpoint configuration:
pub.apply_config([{'uri' => '<myendpoint_uri_1>'}, 
    {'uri' => '<myendpoint_uri_2>'}])

# Remove all configured endpoints:
pub.remove_all_clients

# Explicitly add an endpoint as a PubControlClient instance:
pubclient = PubControlClient.new('<myendpoint_uri>')
# Optionally set JWT auth: pubclient.set_auth_jwt(<claim>, '<key>')
# Optionally set basic auth: pubclient.set_auth_basic('<user>', '<password>')
pub.add_client(pubclient)

# Publish across all configured endpoints:
pub.publish('<channel>', Item.new(HttpResponseFormat.new('Test publish!')))
pub.publish_async('<channel>', Item.new(HttpResponseFormat.new(
    'Test async publish!')), method(:callback))

# Wait for all async publish calls to complete:
pub.finish