abhikalitra
abhikalitra

Reputation: 21

Can MQTT broker be configured to send data to subscribers at a different rate?

I am new to MQTT and working with the mosquitto broker and development libraries.

I have a usecase where different subscribers may want to subscribe to a given topic but would want the data at different rates. So, subscriber1 may want it only on change from the previous value while subscriber2 may want it on a fixed cadence, say, every few mins/seconds.

Is there a way to configure that in the mosquitto broker? Or any other broker that supports it?

Upvotes: 2

Views: 3114

Answers (2)

Craig Conover
Craig Conover

Reputation: 4738

Channel Data Segregation

With or without MQTT, on PubNub you would need two separate channels: one for delta data and one for regular cadence data and there are three strategies you can employ.

Two Channels

The clients wanting only the data changes would subscribe to topic-change. The subscribers wanting a regular cadence of data would subscribe to both topic-cadence and topic-change.

The publisher would publish data at the regular cadence on topic-cadence and if currentValue !=lastValue, publish to topic-change. So both types of subscribers get the data they want.

Stream Filters

Another option with PubNub, you could use Stream Filters to publish a meta-data value equal to "1" if the value has changed from the previous and "0" if the value is the same as the last value published. The values would only be published to one channel, topic-cadence.

pubnub.publish(
    {
        channel: "topic-cadence", 
        message: {"price" : 102.34},
        meta: {"change": "1"}
    },
    function(status, response) {
        // handle publish success/fail here
    }
);

Clients that want all the data would subscribe like normal. But for clients that only want changes, the client would set a filter to only receive messages with that meta-data key set to "1". The client inits PubNub as usual, adds the listener (to receive published messages) and subscribes to the channel topic-cadence, but also sets a filter.

pubnub.setFilterExpression("change=1");
pubnub.subscribe("channels" : ["topic-cadence"]);

And now this client will only have messages sent to it if the data has changed while the client without the filter will get all data that is published at the regular cadence.

Wildcard Channels

Using Wildcard Subscribe the solution is very similar to the first. The publisher will publish values on a channel named topic.cadence as long as the data is the same. If the value changes, it will be published to a channel named topic.delta. The dots (.) in the channel names are important because clients will be able to subscribe using a wildcard - the asterisk (*).

Clients that only want changes in the value will subscribe to topic.change.

pubnub.subscribe("channels" : ["topic.change"]);

And clients that want all of the value in a cadence will subscribe to both of the channels using a wildcard subscribe.

pubnub.subscribe("channels" : ["topic.*"]);

This just means that data published to any channel that starts with topic. and has some sub-channel name - topic.change, topic.foo, topic.bar - will receive that data.

Upvotes: 2

hardillb
hardillb

Reputation: 59608

No, there is no way to configure mosquitto (or any full fledged broker that I am aware of) to do that, it will deliver every message sent to a topic to all subscribers at the rate the publisher sends new messages.

If you really must have this feature then the best you could do is probably take something like mosca (https://github.com/mcollina/mosca) and use the hooks it provides to build your own broker, but it would be a nasty hack and it still wouldn't normally redeliver the same message if no new one had arrived for the constant cadence problem

But it would be so much easier to just keep a little state in the clients to throw duplicates away messages and and event loop that just processed the last message if a new one hadn't arrived yet.

Upvotes: 1

Related Questions