Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[mosquitto-dev] Can publish from mosquitto_pub but not from mosquittopp

Hello, I hope I understood correctly that this is the place for questions with issues not claiming to be bugs.
I found a similar question in the github issues but that one did not really help me.

--------------------------------------------

So I am able to send a message to my demo HiveMQ broker via mosquitto_pub using the .pem file provided by HiveMQ:

On Ubuntu x86 with "mosquitto_pub version 1.6.9 running on libmosquitto 1.6.9.":

$ mosquitto_pub -h <myaddr>.s1.eu.hivemq.cloud -p 8883 -u <myusr> -P <mypwd> -t 'mytopic' -m 'Hello' --cafile ~/Downloads/isrgrootx1.pem -d
Client mosq-9l9QAt2cNQt5sbj3s5 sending CONNECT
Client mosq-9l9QAt2cNQt5sbj3s5 received CONNACK (0)
Client mosq-9l9QAt2cNQt5sbj3s5 sending PUBLISH (d0, q0, r0, m1, 'mytopic', ... (5 bytes))
Client mosq-9l9QAt2cNQt5sbj3s5 sending DISCONNECT

On Ubuntu ARM64 I was unable to do this due to an older version of mosquitto, but after upgrading to mosquitto-dev which yields v2.0.15, it worked without the certificate.

--------------------------------------------

However, on both machines, I haven't managed to publish to my broker via mosquittopp C++ library.
The c++ code did work wit the the public HiveMQ broker at broker.mqttdashboard.com.

I have linked (via CMake) my c++ code to a local build of a freshly cloned mosquitto master branch.

Here are the main function calls i am using (the commented-out constructor for hivemq broker works):

int main()
{
MqttClient mqttClient("myclient", "mytopic", "<myaddr>.s1.eu.hivemq.cloud", 8883);
//MqttClient mqttClient("myclient", "mytopic", "broker.hivemq.com", 1883); // This one works
mqttClient.sendMessage("Hello, MQTT!");
return 0;
}

where the functions are 

MqttClient(const char *id, const char * topic, const char *host, int port) : mosqpp::mosquittopp(id)
{
mosqpp::lib_init();
keepalive_ = 60;
id_ = id;
port_ = port;
host_ = host;
topic_ = topic;
tls_set("~/Downloads/isrgrootx1.pem");
username_pw_set("<myusr>", "<mypwd>");
int ret = connect(host_, port_, keepalive_);
printf("connect() result: %d\n", ret);
ret =loop_start();
printf("loop_start() result: %d\n", ret);
};

~MqttClient()
{
loop_stop();
mosqpp::lib_cleanup();
}


bool sendMessage(const char* message)
{
int ret = publish(NULL, topic_, strlen(message), message, 0, false);

if (ret != MOSQ_ERR_SUCCESS)
{
std::cout << "Error: Unable to send message. Code: " << ret << std::endl;
}
else
{
std::cout << "Message sent: Topic: " << topic_ << " | Message: " << message << std::endl;
}
return (ret == MOSQ_ERR_SUCCESS);
}

As you can see, the on_connect() callback is not called in the case of the private broker.

When I call it with my broker:
connect() result: 0
loop_start() result: 0
Message sent: Topic: mytopic | Message: Hello, MQTT!
Published: 1                              (<<  printed by on_publish() callback)
Disconnecting: 7                       (<<  printed by on_disconnect() callback)
Disconnecting: 7                       (loops on forever)

When I call it with the hivemq broker:
connect() result: 0
loop_start() result: 0
Published: 1  
Message sent: Topic: mytopic | Message: Hello, MQTT!
Connected to MQTT broker.     (<<  printed by on_connect() callback)


Thanks,
Matiss

Back to the top