# IoT_Callbacks

> The eXtremeDB Active Replication Fabric callbacks are functions defined with the structures described below.

> ### For an overview see page ARF Applications in C

> The following structures are used to define callback functions which are registered with `mco_iot_comm_register_callback()` and dropped with `mco_iot_comm_unregister_callback()`:

>         typedef struct mco_iot_read_stream_t_
>         {
>             void            *handle;
>             mco_stream_read  reader;
>         } mco_iot_read_stream_t;

>         typedef struct iot_ack_t_
>         {
>             uint8     seq;
>             uint8     sender_agent_id;
>             uint8     receiver_agent_id;
>             uint8     timestamp;
>             uint4     error_code;
>         } iot_ack_t;

>         typedef struct iot_comm_callback_t_
>         {
>             int (*onConnect)    (mco_iot_connection_h iotc, void *context);
>             int (*onReceive)    (mco_iot_connection_h iotc, mco_iot_read_stream_t *stream, void *context);
>             int (*onAck)        (mco_iot_connection_h iotc, const iot_ack_t *ack, void *context);
>             int (*onDisconnect) (mco_iot_connection_h iotc, void *context);
>             int (*onDestroy)    (mco_iot_connection_h iotc, void *context);
>         } iot_comm_callback_t;

> ### Callback Events

> The callback functions defined in structure `iot_comm_callback_t` are called for the following events:

> onConnect | Called when the a communication is established either through `connect()` or `accept()`
> onReceive | Called when data is received (a set of serialized objects)
> onAck | Called when the acknowledgment for previously sent data (`ACK` message) is received
> onDisconnect | Called when a connection is disconnected
> onDestroy | Called prior to destroying a connection

> ### Usage

> Note that the `void * context` argument is a user-defined context (a pointer to a structure containing application-specific data). Any of the callback functions registered in the `iot_comm_callback_t_` structure must either return `IOT_CALLBACK_OK` (successful completion) or `IOT_CALLBACK_FAIL` (in case of an error, the corresponding connection will be disconnected). Also note that if the callback is not used, it can be set to null.

> ### Example

>         {
>             ...
>             iot_comm_callback_t my_callback = {&my_on_connect, 0, 0, &my_on_disconnect, 0};
>             ...
>             mco_iot_comm_register_callback(comm, &my_callback, my_context);
>         }