Class: SimpleMQTTClient
- Inherits:
-
Object
- Object
- SimpleMQTTClient
- Defined in:
- lib/simple_mqtt_client/simple_mqtt_client.rb
Overview
A simple MQTT client
Instance Method Summary collapse
-
#disconnect ⇒ Object
Disconnects this simple MQTT client instance from the broker.
-
#get_subscribed_channels ⇒ Object
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.
-
#initialize(host, client_id = nil) ⇒ SimpleMQTTClient
constructor
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.
-
#is_channel_wildcard?(channel) ⇒ Boolean
Returns true if a channel is a wildcard channel @return [Boolean] true if the channel is a wildcard channel.
-
#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.
-
#subscribe(channel, callback) ⇒ Object
Subscribes to a channel and registers a callback Single channel callbacks take only one parameter: the received message.
-
#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.
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, | # 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() : cb.call(, channel) } rescue ArgumentError STDERR.puts "The callback you passed for #{channel} has the #{$!}" end end end end |
Instance Method Details
#disconnect ⇒ Object
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_channels ⇒ Object
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
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] the we are publishing
67 68 69 70 71 |
# File 'lib/simple_mqtt_client/simple_mqtt_client.rb', line 67 def publish( channel, ) # 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, ) 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 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 |