I test a sample code and set QOS to 1. Why is the server is always sending the message to the subscribe client even the subscribe client already received the message and as I search https://github.com/njh/ruby-mqtt/pull/58 this should solve the problem because in MQTT specs the subscribe client should send PUBACK after receiving the message to confirm that message is received. Did I missed something? Any tips?
sub.rb
require 'rubygems'
require 'mqtt'
MQTT::Client.connect('192.168.9.105') do |client|
client.get_packet('test'=>1) do |packet|
puts packet.inspect
end
end
pub.rb
require 'rubygems'
require 'mqtt'
# Publish example
MQTT::Client.connect('192.168.9.105') do |c|
c.publish('test', 'message 3', 0, 1)
end
Because MQTT specifications for QoS 1 states that the message is delivered at least once , it may be delivered more than one time. (eg. PUBACK timeout, network issues)
Quoting from the specs link:
Although TCP normally guarantees delivery of packets, there are certain scenarios where an MQTT message may not be received. In the case of MQTT messages that expect a response (QoS >0 PUBLISH, PUBREL, SUBSCRIBE, UNSUBSCRIBE), if the response is not received within a certain time period, the sender may retry delivery. The sender should set the DUP flag on the message.
This can maybe be a bug in the ruby client implementation. The broker is compliant with the specs and re-delivers the message.
If you need the message delivered exactly once try with QoS 2.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.