void myqtt_ctx_set_on_publish ( MyQttCtx ctx,
MyQttOnPublish  on_publish,
axlPointer  user_data 
)

Allows to install a MyQttOnPublish handler on the provided context.

This is a server side handler (broker). Clients publishing messages does not get this handler called.

See myqtt_ctx_set_on_msg and myqtt_conn_set_on_msg for more information about getting notifications when acting as a MQTT client.

See MyQttOnPublish for more information.

You can only configure one handler at time. Calling to configure a handler twice will replace previous one.

Parameters
ctxThe context where the operation will take place.
on_publishThe handler to be configured.
user_dataUser defined pointer to be passed in into the handler every time it gets called.

EXAMPLE 1: Discarding publishing a message to reply to it (request-reply pattern)

The following example shows how to use this handler to block received message so the engine will discard it, but, because you are in control of the connection and the message received, you can reply to it:

1 // Use some pattern to detect which message to reply; in this case we are matching the topic
2 // In this case, we are using this technique to report current client id if the connecting peer
3 // asks for it by publishing to the topic myqtt/admin/get-client-identifier
4 if (axl_cmp ("myqtt/admin/get-client-identifier", myqtt_msg_get_topic (msg))) {
5 
6  // found administrative request, report this information and block publication
7  client_id = myqtt_conn_get_client_id (conn);
8  if (! myqtt_conn_pub (conn, "myqtt/admin/get-client-identifier",
9  (axlPointer) client_id, strlen (client_id), MYQTT_QOS_0, axl_false, 0))
10  printf ("ERROR: failed to publish get-client-identifier..\n");
11 
12  // report received PUBLISH should be discarded
13  return MYQTT_PUBLISH_DISCARD;
14 
15 } // end if

References myqtt_mutex_lock(), and myqtt_mutex_unlock().