Method: MQTT::Client#get

Defined in:
lib/mqtt/client.rb

#getObject

Return the next message received from the MQTT server.

The method either returns the Publish packet:

packet = client.get

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

client.get do |packet|
  # Do stuff here
end


450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
# File 'lib/mqtt/client.rb', line 450

def get
  raise NotConnectedException unless connected?

  loop_start = current_time
  loop do
    packet = @read_queue.pop
    if packet.is_a?(Array) && packet.last >= loop_start
      e = packet.first
      e.set_backtrace((e.backtrace || []) + ['<from MQTT worker thread>'] + caller)
      raise e
    end
    next unless packet.is_a?(Packet)

    unless block_given?
      puback_packet(packet) if packet.qos > 0
      return packet
    end

    yield packet
    puback_packet(packet) if packet.qos > 0
  end
end