back

How to get the online and offline information of device in EMQ

2019-04-02

There are a lot of newcomer for the Internet of Things, MQTT, EMQ X asking how to get the online and offline status of the device. Because such data of the device is a basic representation of IoT data, many higher-level applications will display and analyze based on this data. Mr. EMQ (the author) will introduce how to get the online and offline status of the device from the MQTT protocol side and the Broker side:

General way

· MQTT protocol — Will message mechanism

The Will message mechanism of MQTT protocol can be used to obtain the device status. When the device is connected, the Will is enabled. When connecting to the broker, set the properties such as Will-Topic and Will-Payload. When the client is abnormally disconnected (The device does not send Disconnect packets), the Broker will release the wills Topic and Payload. However, the obvious disadvantages of the will message mechanism is that only the abnormal disconnection will get the offline status.

· MQTT Protocol — Topic Design

There are better tips and solutions to get the status of the device from the MQTT protocol side. You can design on the Topic side of the MQTT protocol for ‘’presence’, such as ‘presence/connect/client-id’. When the device is online, it publishes the online message. When the device is offline normally (The device sends a Disconnect packet), the offline message is published to the topic “presence/disconnect/client-id”.

EMQ X implementation

The above two methods can obtain the online status of the device based on the will message mechanism or topic design of the MQTT protocol. They are applicable for any broker that implements the MQTT protocol. Mr. EMQ will introduce three simple an fast ways to get online status of device from EMQ X Broker.

· EMQ X system topic

EMQ X Broker online and offline status topic:

online topic:$SYS/brokers//clients//connected

offline topic:$SYS/brokers//clients//disconnected

1) can specify the specific node name and device ClientID respectively.

2)Support for ‘+’, ‘#’ wildcards

mosquitto example:

mosquitto_sub -i mosuqitto_test -t ‘$SYS/brokers/+/clients/+/+’ -d

After subscribing to the system’s online and offline topics through the above commands, if the device is online or offline, such messages will be received, and the following message will be printed:

<!--online message> Client mosuqitto_test received PUBLISH (d0, q0, r0, m0, ‘$SYS/brokers/emqx@127.0.0.1/clients/emq/connected’, … (163 bytes)) {“clientid”:”emq”,”username”:”undefined”,”ipaddress”:”127.0.0.1",”connack”:0,”ts”:1538147419,”proto_ver”:3,”proto_name”:”MQIsdp”,”clean_start”:true,”keepalive”:60}

<!--offline message> Client mosuqitto_test received PUBLISH (d0, q0, r0, m0, ‘$SYS/brokers/emqx@127.0.0.1/clients/emq/disconnected’, … (75 bytes)) {“clientid”:”emq”,”username”:”undefined”,”reason”:”closed”,”ts”:1538147420}

· EMQ X Web Hook plug-in

EMQ X Broker officially provides a variety of plug-ins (more detailed available on official website, and emqx-web-hook (emq-web-hook for version 2.0) can bridge the MQTT message to the Web Server specified by the user, including the online and offline status of the device. When the emqx_web_hook is started in the Dashboard plugin management or the terminal emqx_ctl (emqttd_ctl for version 2.0), you can get the status data of the device. The following is an example of bridging the Web server online and offline data through emq-web-hook:

POST / HTTP/1.1 content-type: application/json content-length: 93 te: host: 127.0.0.1:8087 connection: keep-alive

{“action”:”client_connected”,”client_id”:”mqttjs_bc06a34f41",”username”:”admin”,”conn_ack”:0}

offline message:

POST / HTTP/1.1 content-type: application/json content-length: 101 te: host: 127.0.0.1:8087 connection: keep-alive

{“action”:”client_disconnected”,”client_id”:”mqttjs_bc06a34f41",”username”:”admin”,”reason”:”normal”}

· EMQ X Enterprise-Direct access database

The above two methods are supported in the EMQ X open source community version. The commercial version EMQ X Enterprise can store MQTT messages (subscription relationship, device online status, offline messages, and reserved messages) to the backend database (MySQL, PostgreSQL, Cassandra, Redis, MongoDB, and message middleware (Kafka, RabbitMQ) in a more efficiently way. Users can get the status information of the device by querying the relevant data in the database directly .

The design objectives of EMQ X Enterpise:

Obtain high reliability and high availability at the enterprise level;

Support MQTT connection carrying massive IoT terminals;

Support low-latency message routing between massive IoT devices and store MQTT messages efficiently .

For more information on the features of EMQ X Enterprise products, please feel free to contact us.

Summary

That is all for Mr. EMQ’s introduction on how to get the status of the device. For more information about the EMQ X product, please join the official group:

QQ group 1:12222225

QQ group 2:196066320