0

I'm trying to use libmosquitto to make a request (publish to a 'test/topic' topic) and I want to get a response based on the client (sender) id. So that means the client will publish to 'test/topic' and it will automatically subscribe 'test/topic/<client_id>'

The server has already subscribed on 'test/topic' and when it becomes the message, it will send a response (publish) to 'test/topic/<client_id>', which the client subscribed to receive that response in the first place.

The challenge here is how do I get the <client_id>, right. I already done this in python and js, where the client will send metadata or properties in the payload, which the server can unpack to get the client_id. However, I'm using C++ now and it's frustrating because I can't figure out how to get these properties.

Here is an example of how to do this in python. I just want to do the same with c++

I'm using the libmosquitto as I mentionned. I don't even have an example to show because I didn't find how to do this. There is literally no example on how to do this with the mosquitto c++ lib (which is confusing since mosquitto is a famous lib I guess).

I hope someone had a similar problem or can post an example for c++ and mosquitto lib. Thanks in advance.

basilisk
  • 1,156
  • 1
  • 14
  • 34

1 Answers1

1

When in doubt, look at the tests:

const char *my_client_id = ...;
mosquitto_property *proplist = NULL;

mosquitto_property_add_string_pair(&proplist, MQTT_PROP_USER_PROPERTY, "client_id", my_client_id);
mosquitto_publish_v5(mosq, &sent_mid, "test/topic", strlen("message"), "message", 0, false, proplist);
mosquitto_property_free_all(&proplist);

Since you asked in the comments, you can retrieve these properties from published messages by first setting an on_message callback using mosquitto_message_v5_callback_set and the implementing it like so:

void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_message *message, const mosquitto_property *props) {
    std::string topic{message->topic};
    if (topic == "test/topic") {
        const char *client_id = nullptr;
        mosquitto_property_read_string_pair(props, MQTT_PROP_USER_PROPERTY, nullptr, &client_id, false);
        if (client_id) {
            /* client_id contains a client id. */
    }
}
Botje
  • 26,269
  • 3
  • 31
  • 41
  • thanks for the quick answer and the tip. How do I get the client_id on the subscribed client though? that is the problem. When subscribe test/topic from another client, I need to access that proplist in the on_message() method. Do you have any idea how to do this from the subscriber side and not the publisher? – basilisk Aug 06 '21 at 13:11
  • Your question says "I'm trying to use libmosquitto to **make a request**", so that's what I answered. Regardless, if you need to receive these properties on the subscriber, I think it's a simple matter of using [mosquitto_message_v5_callback_set](https://mosquitto.org/api/files/mosquitto-h.html#mosquitto_message_v5_callback_set) and checking the `props`. This was all very clear from the documentation, though. – Botje Aug 06 '21 at 13:14
  • I'm not an expert in C or C++, therefore, I can't wrap my head around this. Ok your example shows how to add a property (client_id) to the message before publishing it, which is great and bring me forward. So now how can I read that property on the subscriber side and get the client_id? is it far from clear in the docs, there is no example for this. Should I access it like props->client_id? or how to read the props exactly – basilisk Aug 06 '21 at 13:24
  • See edit. Also know that there is a built-in property for specifying a response topic, called MQTT_PROP_RESPONSE_TOPIC – Botje Aug 07 '21 at 08:47
  • your example didn't work. I got no errors but my subscriber didn't receive the published message – basilisk Aug 14 '21 at 21:37
  • 1
    after digging in, I noticed my publish_v5 method was not working as expected. I'm getting 10 as return type when i call publish_v5. After searching in the mosquitto.h, I noticed that this is mapped to an error code: MOSQ_ERR_NOT_SUPPORTED. I assume this mean that mqtt v5 is not supported, however, on their website and repo, they are saying that the lib supports v5 already. I'm using the 2.0.11 version, which should work with mqtt v5 – basilisk Aug 14 '21 at 22:14
  • 1
    You must enable v5 on the mosquitto-client by calling ```mosquitto_int_option(mosq, MOSQ_OPT_PROTOCOL_VERSION, MQTT_PROTOCOL_V5);``` before (immediately after creation is probably best). – lambdapower Mar 08 '22 at 16:17