Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [mosquitto-dev] publish in the message callback

Hi,

You really should not be making calls to long running/blocking functions
in the  on_message callback.

The call back is run on the MQTT client's network thread which means
while it is running it can not handle any other messages. This is
includes responding to other incoming messages, handling ping timeouts
to ensure the keepalive period is met.

When you call mosquitto_publish() the message is queued up for the
network thread to handle next, but it can not be processed until the
on_message() function returns.

If you have long running tasks to be run as a result of an incoming
message then you should pass that work off to another thread so the
clients network thread can be returned as soon as possible.

On 17/06/2020 21:05, nomprenom@xxxxxxxxx wrote:
> Hi,
> I want to publish a message to topic B when I receive a message on topic A.
> In the callback of topic A, I use mosquitto_publish("topics/B"....) and
> it works very well.
> 
> My problem is the message arrives at topic B only when I return from the
> callback.
> 
> void on_message(struct mosquitto *mosq, void *obj, const struct
> mosquitto_message *msg)
> {
>         const char *payload = "0000000000000000000000000000";
>         printf("send new msg to B");
>         mosquitto_publish(mosq, NULL, "topic/B", strlen(payload),
> payload, 1, 0);
>         printf ("sleep 10");
>         sleep(10);
>         printf ("wakeup");
> }
> 
> My other process subscribed to B received the message but onlt after 10s
> (=when A print "wakeup")
> 
> Why is it working like this and how to publish from A immediately ?
> 
> I put sleep(10) for example but in reality it's a call to multiple
> functions in my process which take time to execute.
> 
> Thanks.
> 
> 
> _______________________________________________
> mosquitto-dev mailing list
> mosquitto-dev@xxxxxxxxxxx
> To unsubscribe from this list, visit https://www.eclipse.org/mailman/listinfo/mosquitto-dev
> 

-- 
http://www.hardill.me.uk/wordpress
http://about.me/hardillb
http://flickr.com/photos/hardillb/
http://last.fm/user/hardillb
https://keybase.io/hardillb


Back to the top