Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [mosquitto-dev] PUBACK sent after disconnect on publish

mosquitto_publish() is asynchronous: having it return MOSQ_ERR_SUCCESS simply means that the publication of the message has properly been passed to the Mosquitto thread. 
You should use the publish callback to check/wait for message delivery.

See similar question here: https://stackoverflow.com/q/60050736/1051634

Hope this helps,
Benjamin.

Le ven. 21 févr. 2020 à 00:36, matthew stanger <stangerm2@xxxxxxxxx> a écrit :
Hi,

I'm using libMosquitto(pp) with the mosquitto (v1.6.8) broker and when using the cpp wrapper my app end's up disconnecting from the broker (QoS 1) before the broker has sent the PUBACK. This results in 'Socket error on client %s, disconnecting.' and sometimes a message is counted as published, without error, but the broker doesn't actually retain it (retain set).
Here is the short logging capture/example:

1582239917: New client connected from 127.0.0.1 as test_1419364277 (p2, c1, k60).
1582239917: Sending CONNACK to test_1419364277 (0, 0)
1582239917: Received PUBLISH from test_1419364277 (d0, q1, r1, m1, 'device/test', ... (495 bytes))
test: on disconnect
1582239917: Sending PUBACK to test_1419364277 (m1, rc0)
1582239917: Socket error on client test_1419364277, disconnecting.

Where the 'test: on disconnect' message is fired by the mosquittopp:
virtual void on_disconnect(int /*rc*/) {return;}

This is the code I'm using, stripped down for readability.
{
  this->connect (_host, _port, _keepalive)

  publish (NULL, topic, strlen (message), message, _qos, retain);

  while (this->want_write()) {
    this->loop();
    std::this_thread::sleep_for (std::chrono::milliseconds (1));
  }

  // Adding a long sleep here prevents premature disconnect before PUBACK from broker
  // What is the right way, this would be way to hacky to use for real...
  // std::this_thread::sleep_for (std::chrono::seconds (2));

  this->disconnect();
}

Question: Is there a function I should be calling to ensure the PUBACK has been completed or is this implied by the 'publish()' function (bug?).

Cheers,
Matt
_______________________________________________
mosquitto-dev mailing list
mosquitto-dev@xxxxxxxxxxx
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://www.eclipse.org/mailman/listinfo/mosquitto-dev

Back to the top