Method: MQTT::Client#get

Defined in:
lib/mqtt/client.rb

#get(topic = nil, options = {}) ⇒ Object

Return the next message received from the MQTT server. An optional topic can be given to subscribe to.

The method either returns the topic and message as an array:

topic,message = client.get

Or can be used with a block to keep processing messages:

client.get('test') do |topic,payload|
  # Do stuff here
end

402
403
404
405
406
407
408
409
410
411
412
413
414
# File 'lib/mqtt/client.rb', line 402

def get(topic=nil, options={})
  if block_given?
    get_packet(topic) do |packet|
      yield(packet.topic, packet.payload) unless packet.retain && options[:omit_retained]
    end
  else
    loop do
      # Wait for one packet to be available
      packet = get_packet(topic)
      return packet.topic, packet.payload unless packet.retain && options[:omit_retained]
    end
  end
end