RubyDevInc / paho.mqtt.ruby

Eclipse Public License 1.0
31 stars 19 forks source link

Paho persisted connection not reconnecting automatically #25

Open rgaufman opened 6 years ago

rgaufman commented 6 years ago

I ran an experiment, I have this code:

require 'bundler/inline'
gemfile do
  source 'https://rubygems.org'
  gem 'paho-mqtt', github: 'RubyDevInc/paho.mqtt.ruby'
end

### Create a simple client with default attributes
client = PahoMqtt::Client.new({
  host: '127.0.0.1', port: 1883, clean_session: false, persistent: true,
  client_id: 123, username: nil, password: nil,
  will_topic: 'test', will_payload: 'offline', will_qos: 2, will_retain: true,
  keep_alive: 20, ack_timeout: 10
})

### Register a callback on message event to display messages
message_counter = 0
client.on_message do |message|
  puts "Message recieved on topic: #{message.topic} >>> #{message.payload}"
  message_counter += 1
end

### Register a callback on suback to assert the subcription
waiting_suback = true
client.on_suback do
  waiting_suback = false
  puts "Subscribed"
end

waiting_pubcomp_count = 0
client.on_pubcomp do
  waiting_pubcomp_count + 1
  puts "Pubcomp"
end

puts client.connection_state

### Connect to the eclipse test server on port 1883 (Unencrypted mode)
client.connect('127.0.0.1', 1883)

puts client.connection_state

### Subscribe to a topic
client.subscribe(['paho/ruby/test', 2])

### Publlish a message on the topic "paho/ruby/test" with "retain == false" and "qos == 2"
(1..10000).each do |i|
  client.publish("paho/ruby/test", "Hello there #{i}!", true, 2)
  sleep 0.5
end

### Waiting to assert that the message is displayed by on_message callback
loop do
  puts waiting_pubcomp_count
  sleep 5
end

I started mosquito and could see the expected output:

$ ruby test.rb
2
1
Subscribed
Pubcomp
Message recieved on topic: paho/ruby/test >>> Hello there 1!
Pubcomp
Message recieved on topic: paho/ruby/test >>> Hello there 2!
Pubcomp
Message recieved on topic: paho/ruby/test >>> Hello there 3!
Pubcomp
Message recieved on topic: paho/ruby/test >>> Hello there 4!

I then tried killing and restarting mosquitto quickly and paho reconnected. However I then killed mosquitto and ran: sleep 120; mosquitto -c /usr/local/etc/mosquitto/mosquitto.conf -v

After doing this, paho no longer reconnects, and is stuck on:

Message recieved on topic: paho/ruby/test >>> Hello there 38!
Pubcomp
Message recieved on topic: paho/ruby/test >>> Hello there 39!
Pubcomp
Message recieved on topic: paho/ruby/test >>> Hello there 40!

Any ideas what could be the issue?

p-goudet commented 6 years ago

@rgaufman Thank you for this feedback.

In order to prevent the client trying to reconnect forever, default setting limit the reconnection attempt. This is constant that could be set by hand. (PahoMqtt::RECONNECT_RETRY_TIME = 3)

rgaufman commented 6 years ago

Hmm, the thing is it could be an unstable internet connection, so I want paho to retry forever, but it seems to get into a state where it is never able to reconnection until I kill and restart the process manually. Any ideas?