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

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/<node>/clients/<clientid>/connected

offline topic:$SYS/brokers/<node>/clients/<clientid>/disconnected

1)<node>、<clientid> 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@’, … (163 bytes))

<!--offline message>
Client mosuqitto_test received PUBLISH (d0, q0, r0, m0, ‘$SYS/brokers/emqx@’, … (75 bytes))

· 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:

content-type: application/json
content-length: 93
connection: keep-alive


offline message:

content-type: application/json
content-length: 101
connection: keep-alive


· 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.


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