Using Push Notification via MQTT
Follow the implementation steps in this topic to provide push notification using MQTT. Both web app and thing make use of push notification based on these steps. Push notification is provided through these steps too, when you use APIs via MQTT.
This topic assumes you are familiar with the push notification feature of Kii Cloud. See Push Notification for more information about the push notification feature of Kii Cloud.
If you use the Thing-IF SDK and/or the Thing SDK Embedded, you do not need to implement anything additional because the SDKs internally process the push notification.
Kii Cloud can provide push notification with the MQTT protocol.
The specification of the MQTT protocol includes a mechanism for applications to make use of the hierarchical structure of MQTT topics but Kii Cloud does not use this mechanism. Instead, Kii Cloud uses a single MQTT topic to provide push notification.
The figure below indicates the internal structure of Kii Cloud when you provide push notification with MQTT for web apps or things.
In Kii Cloud, you can set up web apps or things as an MQTT subscriber. When an MQTT connection is initialized, Kii Cloud returns one MQTT topic as connection information. A communication channel for push messages is established by subscribing to the MQTT topic by the SUBSCRIBE command. This is indicated as "Subscribe to MQTT" in the above figure.
In addition to the MQTT topic, web app users or things subscribe to buckets and topics on Kii Cloud with the Kii Cloud APIs. This is indicated as "Subscribe to Kii Cloud" in the above figure. When a subscribed bucket is updated or a message arrives to a subscribed topic, such an event is communicated to the subscriber via the communication channel established with the MQTT topic.
Subscribing to an MQTT topic establishes only one communication channel per a connection. Subscriptions to multiple buckets and/or topics on Kii Cloud are collected in one MQTT topic and events are notified via the MQTT topic. When a push notification arrives, detailed information of the event source such as the bucket name and topic ID is available in the payload of the PUBLISH command.
This document distinguishes topics of Kii Cloud and MQTT topics of the MQTT protocol. As explained above, these are different concepts.
The operation is the same when there are multiple subscribers as shown in the figure below. One MQTT connection is established to one MQTT topic.
When a subscriber establishes a connection to Kii Cloud, it subscribes to an MQTT topic which corresponds to its connection. The subscriber also subscribes to buckets and/or topics with the Kii Cloud APIs. When events occur in any of those buckets and topics, those events are collected in the MQTT topic for the subscriber and communicated to the subscriber as push notification messages.
Follow these steps to make use of push notification.
Get connection information for MQTT
To connect to the MQTT broker, you need various pieces of information including the servername, port number, username, password, and MQTT topic name for push notification.
You can get such connection information through the API. If you use any SDK, you can get the connection information with an HTTPS request. If you use the API via MQTT, you can get the connection information by onboarding to the default MQTT broker.
Connect to the MQTT broker
Connect to the MQTT broker with the obtained information using an MQTT library.
Subscribe to an MQTT topic and wait for notifications
Send the SUBSCRIBE command of the MQTT protocol to subscribe to an MQTT topic for push notification.
The PUBLISH command for the subscribed MQTT topic arrives when an event occurs on the server side.
See the below diagram which illustrates Step 2 and 3 of the sequence.
After subscribing to an MQTT topic on Kii Cloud with the SUBSCRIBE command, basically, your app will just wait for PUBLISH commands from Kii Cloud. When an event occurs on Kii Cloud, your app will receive a PUBLISH command and get the detail of the event from the command payload.
Currently, the supported QoS level is 0. So, any responses such as a PUBACK command against a PUBLISH command are unnecessary.
If you use the push notification feature only, you do not need to develop a process to receive the SUBSCRIBE command or to send the PUBLISH command. Kii Cloud does not subscribe to an MQTT topic of clients.
Depending on the library of your choice, you might need to deveop a process to send the PINREQ command to implement the keep alive timer.