Skip to content

Commit

Permalink
Assume publish response as ping response
Browse files Browse the repository at this point in the history
This option should be needed due to high frequently traffic environment.

When flood of messages are reached in this client,
keep_alive! response may not reached until response timeout and
causes MQTT::ProtocolException with "No Ping Response received for
xxx seconds".

Signed-off-by: Hiroshi Hatake <[email protected]>
  • Loading branch information
cosmo0920 committed Dec 11, 2020
1 parent d1a9ad7 commit 194a675
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/mqtt/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ class Client
# Last ping response time
attr_reader :last_ping_response

# Assume publish response as ping response
attr_accessor :assume_publish_response_as_pingresp

# Timeout between select polls (in seconds)
SELECT_TIMEOUT = 0.5

Expand All @@ -74,7 +77,8 @@ class Client
:will_payload => nil,
:will_qos => 0,
:will_retain => false,
:ssl => false
:ssl => false,
:assume_publish_response_as_pingresp => false
}

# Create and connect a new MQTT Client
Expand Down Expand Up @@ -486,6 +490,7 @@ def handle_packet(packet)
if packet.class == MQTT::Packet::Publish
# Add to queue
@read_queue.push(packet)
@last_ping_response = Time.now if @assume_publish_response_as_pingresp
elsif packet.class == MQTT::Packet::Pingresp
@last_ping_response = Time.now
elsif packet.class == MQTT::Packet::Puback
Expand Down
10 changes: 10 additions & 0 deletions spec/mqtt_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
expect(client.port).to eq(1883)
expect(client.version).to eq('3.1.1')
expect(client.keep_alive).to eq(15)
expect(client.assume_publish_response_as_pingresp).to be_falsey
end

it "with a single string argument, it should use it has the host" do
Expand Down Expand Up @@ -81,6 +82,15 @@
expect(client.keep_alive).to eq(65)
end

it "with a combination of a host name, port and a hash of settings with different pattern" do
client = MQTT::Client.new('localhost', 1888, :keep_alive => 65,
:assume_publish_response_as_pingresp => true)
expect(client.host).to eq('localhost')
expect(client.port).to eq(1888)
expect(client.keep_alive).to eq(65)
expect(client.assume_publish_response_as_pingresp).to be_truthy
end

it "with a mqtt:// URI containing just a hostname" do
client = MQTT::Client.new(URI.parse('mqtt://mqtt.example.com'))
expect(client.host).to eq('mqtt.example.com')
Expand Down

0 comments on commit 194a675

Please sign in to comment.