Class: MQTT::Client
- Inherits:
-
Object
- Object
- MQTT::Client
- Defined in:
- lib/em-mqtt/client.rb.new.rb
Overview
Client class for talking to an MQTT broker
Instance Attribute Summary collapse
-
#ack_timeout ⇒ Object
Number of seconds to wait for acknowledgement packets.
-
#clean_start ⇒ Object
Set the ‘Clean Start’ flag when connecting?.
-
#client_id ⇒ Object
Client Identifier.
-
#keep_alive ⇒ Object
Time (in seconds) between pings to remote broker.
-
#remote_host ⇒ Object
readonly
Hostname of the remote broker.
-
#remote_port ⇒ Object
readonly
Port number of the remote broker.
Class Method Summary collapse
Instance Method Summary collapse
-
#connect ⇒ Object
Connect to the MQTT broker A block must be given.
-
#connected? ⇒ Boolean
Checks whether the client is connected to the broker.
-
#disconnect(send_msg = true) ⇒ Object
Disconnect from the MQTT broker.
-
#get(*topics) ⇒ Object
Return the next message recieved from the MQTT broker.
-
#initialize(remote_host = MQTT::DEFAULT_HOST, remote_port = MQTT::DEFAULT_PORT) ⇒ Client
constructor
Create a new MQTT Client instance.
-
#publish(topic, payload, retain = false, qos = 0) ⇒ Object
Publish a message on a particular topic to the MQTT broker.
-
#subscribe(*topics) ⇒ Object
Send a subscribe message for one or more topics on the MQTT broker.
-
#unsubscribe(*topics) ⇒ Object
Send a unsubscribe message for one or more topics on the MQTT broker.
Constructor Details
#initialize(remote_host = MQTT::DEFAULT_HOST, remote_port = MQTT::DEFAULT_PORT) ⇒ Client
Create a new MQTT Client instance
11 12 13 14 15 16 17 18 19 |
# File 'lib/em-mqtt/client.rb.new.rb', line 11 def initialize(remote_host=MQTT::DEFAULT_HOST, remote_port=MQTT::DEFAULT_PORT) @remote_host = remote_host @remote_port = remote_port @keep_alive = 10 @clean_start = true @client_id = nil @ack_timeout = 5 @connection = nil end |
Instance Attribute Details
#ack_timeout ⇒ Object
Number of seconds to wait for acknowledgement packets
8 9 10 |
# File 'lib/em-mqtt/client.rb.new.rb', line 8 def ack_timeout @ack_timeout end |
#clean_start ⇒ Object
Set the ‘Clean Start’ flag when connecting?
6 7 8 |
# File 'lib/em-mqtt/client.rb.new.rb', line 6 def clean_start @clean_start end |
#client_id ⇒ Object
Client Identifier
7 8 9 |
# File 'lib/em-mqtt/client.rb.new.rb', line 7 def client_id @client_id end |
#keep_alive ⇒ Object
Time (in seconds) between pings to remote broker
5 6 7 |
# File 'lib/em-mqtt/client.rb.new.rb', line 5 def keep_alive @keep_alive end |
#remote_host ⇒ Object (readonly)
Hostname of the remote broker
3 4 5 |
# File 'lib/em-mqtt/client.rb.new.rb', line 3 def remote_host @remote_host end |
#remote_port ⇒ Object (readonly)
Port number of the remote broker
4 5 6 |
# File 'lib/em-mqtt/client.rb.new.rb', line 4 def remote_port @remote_port end |
Class Method Details
.connect(*args, &blk) ⇒ Object
21 22 23 |
# File 'lib/em-mqtt/client.rb.new.rb', line 21 def self.connect(*args, &blk) self.new(*args).connect(&blk) end |
Instance Method Details
#connect ⇒ Object
Connect to the MQTT broker A block must be given
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/em-mqtt/client.rb.new.rb', line 27 def connect #trap("INT") {EM.stop} EventMachine.run do @connection = MQTT::ClientConnection.connect(@remote_host, @remote_port) # Stuff happens here yield(self) # Disconnect once all the other deferred callbacks @connection.callback do disconnect end puts "Finished initing run" end end |
#connected? ⇒ Boolean
Checks whether the client is connected to the broker.
52 53 54 55 |
# File 'lib/em-mqtt/client.rb.new.rb', line 52 def connected? # FIXME: check if connection is connected not @connection.nil? end |
#disconnect(send_msg = true) ⇒ Object
Disconnect from the MQTT broker. If you don’t want to say goodbye to the broker, set send_msg to false.
47 48 49 |
# File 'lib/em-mqtt/client.rb.new.rb', line 47 def disconnect(send_msg=true) @connection.disconnect(send_msg) unless @connection.nil? end |
#get(*topics) ⇒ Object
Return the next message recieved from the MQTT broker. This method blocks until a message is available.
The method returns the topic and message as an array:
topic, = client.get
86 87 88 |
# File 'lib/em-mqtt/client.rb.new.rb', line 86 def get(*topics) # FIXME: implement this end |
#publish(topic, payload, retain = false, qos = 0) ⇒ Object
Publish a message on a particular topic to the MQTT broker.
58 59 60 61 |
# File 'lib/em-mqtt/client.rb.new.rb', line 58 def publish(topic, payload, retain=false, qos=0) # FIXME: make sure @connection isn't nil @connection.publish(topic, payload, retain, qos=0) end |
#subscribe(*topics) ⇒ Object
Send a subscribe message for one or more topics on the MQTT broker. The topics parameter should be one of the following:
-
String: subscribe to one topic with QOS 0
-
Array: subscribe to multiple topics with QOS 0
-
Hash: subscribe to multiple topics where the key is the topic and the value is the QOS level
For example:
client.subscribe( 'a/b' )
client.subscribe( 'a/b', 'c/d' )
client.subscribe( ['a/b',0], ['c/d',1] )
client.subscribe( 'a/b' => 0, 'c/d' => 1 )
75 76 77 78 |
# File 'lib/em-mqtt/client.rb.new.rb', line 75 def subscribe(*topics) # FIXME: make sure @connection isn't nil @connection.subscribe(*topics) end |
#unsubscribe(*topics) ⇒ Object
Send a unsubscribe message for one or more topics on the MQTT broker
91 92 93 94 |
# File 'lib/em-mqtt/client.rb.new.rb', line 91 def unsubscribe(*topics) # FIXME: make sure @connection isn't nil @connection.unsubscribe(*topics) end |