Skip to content
This repository was archived by the owner on Aug 4, 2020. It is now read-only.
This repository was archived by the owner on Aug 4, 2020. It is now read-only.

subscribe / on_subscribe #14

Description

@dramalho

Hi

quick check, maybe I'm being an idiot here :)

I'm using them gem to connect to a mosquitto instance running locally . I'm building a bunch of agent that will be running for a while so I'm trying to be somewhat resilient here and because everything runs asynchronously, so I have, for instance, a callback on on_connect to flush any topic subscriptions that other parts of the agent might have requested before mosquitto was actually connected.

For that, and following the documentation, I'm assigning each topic a numerical id that I provide when subscribing to the topic, I then have a callback block on on_subscribe that I would use to mark a certain topic as subscribed (and if for some reason flush_topics would need to run again, I would skip those)

Anyway, my issue is I'm assigning topic subscription an id, but the callback responds with a completely different number, so I can't match both .

Some code might help here :) , this is a simplification of what I'm doing, but it's fine

require 'mosquitto'

mqtt_host = 'localhost'
mqtt_port = 1883
client_id = Process.pid.to_s

mqtt = Mosquitto::Client.new # default options

subscribed_topics = [
  {topic: 'topic_1', subscribed: false},
  {topic: 'topic_2', subscribed: false},
  {topic: 'topic_3', subscribed: false},
]

mqtt.on_connect do |_|
  puts 'Mqtt on_connect'

  # flush topics
  subscribed_topics.each_with_index do |t, k|
    puts "Subscribing to #{t[:topic]} with mid #{k+100}"
    mqtt.subscribe(k + 100, t[:topic], Mosquitto::AT_LEAST_ONCE) unless t[:subscribed]
  end  
end

mqtt.on_subscribe do |mid, qos|
  puts "Mqtt on_subscribe: #{mid} -> #{qos}"
  subscribed_topics[mid][:subscribed] = true if subscribed_topics[mid]
end

mqtt.loop_start
mqtt.connect_async(mqtt_host, mqtt_port, 10)

and this outputs the following

Mqtt on_connect
Subscribing to topic_1 with mid 100
Subscribing to topic_2 with mid 101
Subscribing to topic_3 with mid 102
Mqtt on_subscribe: 1 -> [0]
Mqtt on_subscribe: 3 -> [0]
Mqtt on_subscribe: 2 -> [0]

Now, my expectation from the documentation on subscribe and on_subscribe would be that the mid was there to achieve this sort of async matching, but the behaviour seems to indicate that the subscribe method is ignoring the given mid and applying a sequencial mid that the on_subscribe then reports (or something) :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions