Method: MQTT::Client#subscribe

Defined in:
lib/mqtt/client.rb

#subscribe(*topics, wait_for_ack: false) ⇒ Object

Send a subscribe message for one or more topics on the MQTT server. 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 } )


413
414
415
416
417
418
419
420
421
422
423
# File 'lib/mqtt/client.rb', line 413

def subscribe(*topics, wait_for_ack: false)
  raise NotConnectedException unless connected?

  packet = MQTT::Packet::Subscribe.new(
    id: next_packet_id,
    topics: topics
  )
  token = register_for_ack(packet) if wait_for_ack
  send_packet(packet)
  wait_for_ack(token) if wait_for_ack
end