class CelluloidPubsub::Client

worker that subscribes to a channel or publishes to a channel if it used to subscribe to a channel the worker will dispatch the messages to the actor that made the connection in the first place.

@!attribute actor

@return [Celluloid::Actor] actor to which callbacks will be delegated to

@!attribute options

@return [Hash] the options that can be used to connect to webser and send additional data

@!attribute channel

@return [String] The channel to which the client will subscribe to

Attributes

actor[RW]

The actor that made the connection @return [Celluloid::Actor] actor to which callbacks will be delegated to

channel[RW]

The channel to which the client will subscribe to once the connection is open @return [String] The channel to which the client will subscribe to

options[RW]
options that can be used to connect to webser and send additional data

@return [Hash] the options that can be used to connect to webser and send additional data

Public Class Methods

new(options) click to toggle source
receives a list of options that are used to connect to the webserver and an actor to which the callbacks are delegated to
when receiving messages from a channel

@param [Hash] options the options that can be used to connect to webser and send additional data @option options [String] :actor The actor that made the connection @option options [String] :channel The channel to which the client will subscribe to once the connection is open @option options [String] :log_file_path The path to the log file where debug messages will be printed, otherwise will use STDOUT @option options [String]:hostname The hostname on which the webserver runs on @option options [String] :port The port on which the webserver runs on @option options [String] :path The request path that the webserver accepts

@return [void]

@api public

# File lib/celluloid_pubsub/client.rb, line 50
def initialize(options)
  @options = options.stringify_keys!
  @actor ||= @options.fetch('actor', nil)
  @channel ||= @options.fetch('channel', nil)
  @shutting_down = false
  raise "#{self}: Please provide an actor in the options list!!!" if @actor.blank?
  setup_celluloid_logger
  log_debug "#{@actor.class} starting on #{hostname}:#{port}"
  supervise_actors
end

Public Instance Methods

connection() click to toggle source

the method will return the client that is used to

@return [Celluloid::WebSocket::Client] the websocket connection used to connect to server

@api public

# File lib/celluloid_pubsub/client.rb, line 106
def connection
  @connection ||= CelluloidPubsub::ClientConnection.new("ws://#{hostname}:#{port}#{path}", Actor.current)
end
debug_enabled?() click to toggle source
checks if debug is enabled

@return [boolean]

@api public

# File lib/celluloid_pubsub/client.rb, line 158
def debug_enabled?
  @options.fetch('enable_debug', false).to_s == 'true'
end
hostname() click to toggle source

the method will return the hostname of the server

@return [String] the hostname where the server runs on

@api public

# File lib/celluloid_pubsub/client.rb, line 116
def hostname
  @hostname ||= @options.fetch('hostname', CelluloidPubsub::WebServer::HOST)
end
log_file_path() click to toggle source

the method will return the path to the log file where debug messages will be printed

@return [String, nil] return the path to the log file where debug messages will be printed

@api public

# File lib/celluloid_pubsub/client.rb, line 76
def log_file_path
  @log_file_path ||= @options.fetch('log_file_path', nil)
end
log_level() click to toggle source

the method will return the log level of the logger

@return [Integer, nil] return the log level used by the logger ( default is 1 - info)

@api public

# File lib/celluloid_pubsub/client.rb, line 85
def log_level
  @log_level ||= @options['log_level'] || ::Logger::Severity::INFO
end
on_close(code, reason) click to toggle source

callback executes when connection closes

@param [String] code

@param [String] reason

@return [void]

@api public

# File lib/celluloid_pubsub/client.rb, line 255
def on_close(code, reason)
  log_debug("#{self.class} dispatching on close  #{code} #{reason}")
  if @actor.respond_to?(:async)
    @actor.async.on_close(code, reason)
  else
    @actor.on_close(code, reason)
  end
ensure
  log_debug("#{self.class} closing the connection on close and terminating")
  connection.terminate unless actor_dead?(connection)
  terminate
end
on_message(data) click to toggle source

callback executes when actor receives a message from a subscribed channel and parses the message using JSON.parse and dispatches the parsed message to the original actor that made the connection

@param [JSON] data

@return [void]

@api public

# File lib/celluloid_pubsub/client.rb, line 236
def on_message(data)
  message = JSON.parse(data)
  log_debug("#{@actor.class} received JSON  #{message}")
  if @actor.respond_to?(:async)
    @actor.async.on_message(message)
  else
    @actor.on_message(message)
  end
end
on_open() click to toggle source
callback executes after connection is opened and delegates action to actor

@return [void]

@api public

# File lib/celluloid_pubsub/client.rb, line 222
def on_open
  log_debug("#{@actor.class} websocket connection opened")
  async.subscribe(@channel) if @channel.present?
end
path() click to toggle source

the method will return the path of the URL on which the servers acccepts the connection

@return [String] the URL path that the server is mounted on

@api public

# File lib/celluloid_pubsub/client.rb, line 136
def path
  @path ||= @options.fetch('path', CelluloidPubsub::WebServer::PATH)
end
port() click to toggle source

the method will return the port on which the server accepts connections

@return [String] the port on which the server accepts connections

@api public

# File lib/celluloid_pubsub/client.rb, line 126
def port
  @port ||= @options.fetch('port', nil) || CelluloidPubsub::WebServer.find_unused_port
end
publish(channel, data) click to toggle source

publishes to a channel some data (can be anything)

@param [string] channel @param [#to_s] data

@return [void]

@api public

# File lib/celluloid_pubsub/client.rb, line 182
def publish(channel, data)
  send_action('publish', channel, data)
end
shutdown() click to toggle source

the method will terminate the current actor

@return [void]

@api public

# File lib/celluloid_pubsub/client.rb, line 146
def shutdown
  @shutting_down = true
  log_debug "#{self.class} tries to 'shutdown'"
  terminate
end
shutting_down?() click to toggle source

the method will return true if the actor is shutting down

@return [Boolean] returns true if the actor is shutting down

@api public

# File lib/celluloid_pubsub/client.rb, line 67
def shutting_down?
  @shutting_down == true
end
subscribe(channel, data = {}) click to toggle source

subscribes to a channel . need to be used inside the connect block passed to the actor

@param [string] channel

@return [void]

@api public

# File lib/celluloid_pubsub/client.rb, line 169
def subscribe(channel, data = {})
  log_debug("#{@actor.class} tries to subscribe to channel  #{channel}")
  async.send_action('subscribe', channel, data)
end
supervise_actors() click to toggle source

the method will link the current actor to the actor that is attached to, and the connection to the current actor

@return [void]

@api public

# File lib/celluloid_pubsub/client.rb, line 94
def supervise_actors
  current_actor = Actor.current
  @actor.link current_actor if @actor.respond_to?(:link)
  current_actor.link connection
end
unsubscribe(channel) click to toggle source

unsubscribes current client from a channel

@param [string] channel

@return [void]

@api public

# File lib/celluloid_pubsub/client.rb, line 193
def unsubscribe(channel)
  send_action('unsubscribe', channel)
end
unsubscribe_all() click to toggle source

unsubscribes all clients from all channels

@return [void]

@api public

# File lib/celluloid_pubsub/client.rb, line 213
def unsubscribe_all
  send_action('unsubscribe_all')
end
unsubscribe_clients(channel) click to toggle source

unsubscribes all clients subscribed to a channel

@param [string] channel

@return [void]

@api public

# File lib/celluloid_pubsub/client.rb, line 204
def unsubscribe_clients(channel)
  send_action('unsubscribe_clients', channel)
end

Private Instance Methods

actor_died(actor, reason) click to toggle source

method called when the actor is exiting

@param [actor] actor - the current actor @param [Hash] reason - the reason it crashed

@return [void]

@api public

# File lib/celluloid_pubsub/client.rb, line 308
def actor_died(actor, reason)
  @shutting_down = true
  log_debug "Oh no! #{actor.inspect} has died because of a #{reason.class}"
end
chat(message) click to toggle source

method used to send messages to the webserver checks too see if the message is a hash and if it is it will transform it to JSON and send it to the webser otherwise will construct a JSON object that will have the key action with the value 'messageā€œ and the key message witth the parameter's value

@param [Hash] message

@return [void]

@api private

# File lib/celluloid_pubsub/client.rb, line 294
def chat(message)
  final_message = message.is_a?(Hash) ? message.to_json : JSON.dump(action: 'message', message: message)
  log_debug("#{@actor.class} sends JSON #{final_message}")
  connection.text final_message
end
send_action(action, channel = nil, data = {}) click to toggle source

method used to send an action to the webserver reactor , to a chanel and with data

@param [String] action @param [String] channel @param [Hash] data

@return [void]

@api private

# File lib/celluloid_pubsub/client.rb, line 279
def send_action(action, channel = nil, data = {})
  data = data.is_a?(Hash) ? data : {}
  publishing_data = { 'client_action' => action, 'channel' => channel, 'data' => data }.reject { |_key, value| value.blank? }
  async.chat(publishing_data)
end