Class: SimpleMQTTClient

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_mqtt_client/simple_mqtt_client.rb

Overview

A simple MQTT client

Instance Method Summary collapse

Constructor Details

#initialize(host, client_id = nil) ⇒ SimpleMQTTClient

Creates a new MQTT client

@param [String] host the hostname of the MQTT broker we are connecting to
@param [String] client_id the **unique** client identifier


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/simple_mqtt_client/simple_mqtt_client.rb', line 15

def initialize( host, client_id=nil )
  @channels = Hash.new
  @client = client_id.nil? ? MQTT::Client.connect(:host => host) : MQTT::Client.connect(host: host, client_id: client_id)
  @thread = Thread.new('mqtt') do
    @client.get do |channel, message|
      # Execute all the appropriate callbacks:
      # the ones specific to this channel with a single parameter (message)
      # the ones associated to a wildcard channel, with two parameters (message and channel)
      cbs = get_callbacks channel
      begin
        cbs.each { |cb| cb.parameters.length==1 ? cb.call(message) : cb.call(message, channel) }
      rescue ArgumentError
        STDERR.puts "The callback you passed for #{channel} has the #{$!}"
      end
    end
  end
end

Instance Method Details

#disconnectObject

Disconnects this simple MQTT client instance from the broker



74
75
76
77
78
# File 'lib/simple_mqtt_client/simple_mqtt_client.rb', line 74

def disconnect
  @thread.exit
  @client.disconnect
  @channels.clear
end

#get_subscribed_channelsObject

Returns a hash of all the channels this client is currently subscribed to with relative callbacks

@return [Hash] all channels this client is currently subscribed to, and relative callbacks


82
83
84
# File 'lib/simple_mqtt_client/simple_mqtt_client.rb', line 82

def get_subscribed_channels
  @channels
end

#is_channel_wildcard?(channel) ⇒ Boolean

Returns true if a channel is a wildcard channel

@return [Boolean] true if the channel is a wildcard channel. See MQTT specification for wildcard channels

here

Parameters:

  • channel (String)

    the channel we are testing for wildcard

Returns:

  • (Boolean)


90
91
92
# File 'lib/simple_mqtt_client/simple_mqtt_client.rb', line 90

def is_channel_wildcard?( channel )
  channel.include?('#') || channel.include?('+')
end

#publish(channel, message) ⇒ Object

Publishes a message to a channel

@param [String] channel the channel we are publishing to
@param [String] message the message we are publishing


67
68
69
70
71
# File 'lib/simple_mqtt_client/simple_mqtt_client.rb', line 67

def publish( channel, message )
  # Check we are not publishing to a wildcard channel
  STDERR.puts 'Can\'t publish to a wildcard channel!' if is_channel_wildcard? channel
  @client.publish(channel, message)
end

#subscribe(channel, callback) ⇒ Object

Subscribes to a channel and registers a callback Single channel callbacks take only one parameter: the received message. Wildcard callbacks take two parameters: the received message and the channel the message was sent to. It is possible to register multiple callbacks per channel. All of them will be executed whenever a message on that channel is received. Note that overlaps between channel-specific callbacks and wildcard-filters are allowed.

@param [String] channel the channel or filter we are subscribing to
@param [Proc] callback the callback that gets called
whenever a messages is received


42
43
44
45
46
47
48
49
# File 'lib/simple_mqtt_client/simple_mqtt_client.rb', line 42

def subscribe( channel, callback )
  if @channels.include?(channel)
    @channels[channel] << callback
  else
    @channels[channel]=[callback]
    @client.subscribe channel
  end
end

#unsubscribe(channel, callback) ⇒ Object

Un-subscribes a specific callback from a channel

@param [String] channel the channel we are un-subscribing from
@param [Proc] callback the specific callback we want to remove


54
55
56
57
58
59
60
61
62
# File 'lib/simple_mqtt_client/simple_mqtt_client.rb', line 54

def unsubscribe( channel, callback )
  if @channels.include? channel
    @channels[channel].delete(callback)
  end
  if @channels[channel].empty?
    @client.unsubscribe channel
    @channels.delete(channel)
  end
end