[Interest] MQTT Client Message Routing

Andrew Ialacci andrew at dkai.dk
Mon Jan 29 12:21:49 CET 2018


Hello Qt Friends!

When using the QMqtt library, I've noticed there are two possible ways to inspect messages received by the client from the broker.

1. QMqttClient::messageReceived(const QByteArray &message, const QMqttTopicName &topic)
2. QMqttSubscription::messageReceived(const QMqttMessage & message)

I understand that the first will be a firehose of all messages being received by the client. The second is to only see messages having to do with a specific subscription.

Consider the following scenario:

// Example Topic Hierarchy
- app/users/$user/$client/friends
- app/users/$user/$client/conversations
- app/users/$user/$client/events

// Questions:

- Is it better to maintain individual QMqttSubscription objects for all possible topics a client is interested in and connect to each QMqttSubscription::messageReceived signal?

- Or does it make more sense to have a single subscription to a root topic (where applicable, in this case: "app/users/$user/$client/+" ), a bunch of IFs, and QMqttTopicFilter::match(const QMqttTopicName &name) to handle each case?

- It feels like maintaining a huge list of subscription objects will get annoying. However, is doing so worth it in terms of performance? Does it somehow reduce the overhead of matching strings all over (either internally in the QMqttClient or by my calling QMqttTopicFilter::match)?

// Example of "switching" on topic in a QMqttSubscription::messageReceived handler
// using QMqttTopicFilter::match.
// Note:
//   Please ignore syntax / coding / technical errors.
//   This was written freehand purely to illustrate my point.

void AppMessageRouter::handleMQTTMessageReceived(const QMqttMessage &mqttMessage)
{
    if (QMqttTopicFilter("app/users/$user/$client/friends").match(mqttMessage.topic()) == true)
    {
        this->routeFriendsMessage(mqttMessage);
        return;
    }

    if (QMqttTopicFilter("app/users/$user/$client/conversations").match(mqttMessage.topic()) == true)
    {
        this-> routeConversationsMessage(mqttMessage);
        return;
    }

    if (QMqttTopicFilter("app/users/$user/$client/ events ").match(mqttMessage.topic()) == true)
    {
        this->routeEventMessage(mqttMessage);
        return;
    }
}

Thanks for your time!
🍍







More information about the Interest mailing list