thingsboard / tbmq

Open-source, scalable, and fault-tolerant MQTT broker able to handle 4M+ concurrent client connections, supporting at least 3M messages per second throughput per single cluster node with low latency delivery. The cluster mode supports more than 100M concurrently connected clients.
https://thingsboard.io/products/mqtt-broker/
Apache License 2.0
570 stars 46 forks source link

How to help business systems perceive whether credentials are online in real time #127

Closed baigod closed 1 month ago

baigod commented 2 months ago

Component

Description We maintain a list of devices in our business system and create unique MQTT credentials for each device, matching them one-to-one with TBMQ credentials. Now, we want our business system to be able to know in real-time whether a device is online. What are the best practices or solutions for interacting with TBMQ to achieve this?

Environment

dmytro-landiak commented 2 months ago

Hi @baigod!

Thank you for reaching out! You can achieve real-time monitoring of device statuses using the TBMQ API. The API allows you to check the online status of a device by querying its client session. Here's an example of how to do this using a curl command:

curl -X 'GET' \
  'https://HOSTNAME/api/client-session?clientId=CLIENT_ID' \
  -H 'accept: application/json' \
  -H 'X-Authorization: Bearer TOKEN'

Replace the HOSTNAME to your actual one and the CLIENT_ID parameter with the actual client ID of the device you want to check. The X-Authorization header should contain a valid JWT token for authentication.

In addition, you can choose another API like below:

curl -X 'GET' \
  'https://HOSTNAME/api/client-session?pageSize=100&page=0' \
  -H 'accept: application/json' \
  -H 'X-Authorization: Bearer TOKEN'

If you need further assistance or have any questions, feel free to ask!

baigod commented 2 months ago

Hi @baigod!

Thank you for reaching out! You can achieve real-time monitoring of device statuses using the TBMQ API. The API allows you to check the online status of a device by querying its client session. Here's an example of how to do this using a curl command:

curl -X 'GET' \
  'https://HOSTNAME/api/client-session?clientId=CLIENT_ID' \
  -H 'accept: application/json' \
  -H 'X-Authorization: Bearer TOKEN'

Replace the HOSTNAME to your actual one and the CLIENT_ID parameter with the actual client ID of the device you want to check. The X-Authorization header should contain a valid JWT token for authentication.

In addition, you can choose another API like below:

curl -X 'GET' \
  'https://HOSTNAME/api/client-session?pageSize=100&page=0' \
  -H 'accept: application/json' \
  -H 'X-Authorization: Bearer TOKEN'

If you need further assistance or have any questions, feel free to ask!

Thank you for your reply, but I think I prefer events like Connect Event and Disconnect Event in thingsboard. I can proactively inform the business system through Kafka or HTTP. Based on my estimation, when I have millions of devices connected to TBMQ, if I call through HTTP https://HOSTNAME/api/client-session The interface and timeliness may suffer significant losses. I have an idea. Is it feasible for me to subscribe to topics such as tbmq.client.session. through a new subscription group to consume tbmq's session events, so that the business system can perceive real-time changes in device connection status?

dmytro-landiak commented 2 months ago

@baigod,

Thank you for your feedback. Your approach of subscribing to tbmq.client.session topic for real-time session events is indeed better suited for your use case, especially with a large number of devices.

You can connect to Kafka with a separate process, creating a new consumer group to read from the tbmq.client.session topic. Please be aware of the payload format. We use Protobuf for serialization. You can find the object definition for ClientSessionInfoProto here.

I believe you understand the importance of staying up-to-date with the schema definition, which may not be the most convenient option. However, currently, we do not have any other mechanisms to send notifications about the status changes of the devices.

dmytro-landiak commented 1 month ago

I am closing this issue due to inactivity. Please feel free to reopen it if you have any more questions.