model/dedup Cotonic

model.dedup

The dedup model implements message deduplication. It works by sending a message to the dedup model, the dedup model then relays the message to a topic with a response topic set to the dedup model.

If a message is tried to be relayed before a message is received on the response topic then it will be saved. Only the last saved message is stored. Any previously saved messages are overwritten.

If a message is received on the dedup response topic then that message will be relayed to the original message's response topic (if any). If there is a saved message then that message will be relayed to the saved message topic.

post/message
Handles messages with the following payload:

{
    topic: "some/topic",
    payload: "the message payload",
    timeout: optionalTimeoutInMsec
}

The payload will be published to the given topic. The timeout is optional and defaults to 15000 msec. If there is no response to the dedup reponse topic within the timeout then the message is assumed to be lost and any queued message or new message is allowed to be relayed.
The de-duplication is done by generating a key. The key is the concatenation of the topic and the response topic of the message.

self.publish("model/dedup/post/message", { topic: "hello/there", payload: { a: 1 } })
=> De-duplicates messages to the hello/there topic.

post/message/+key
Exactly like post/message except that the deduplication key is not generated from the topics but predefined. This enables to use deduplication in calls, where the response topic is always unique.

self.call("model/dedup/post/message/one-at-a-time", { topic: "hello/there", payload: { a: 1 } })
.then( (msh) => console.log(msg) );
=> De-duplicates all messages using the one-at-a-time key.

event/ping
When the dedup model is enabled a retained message is published on the model/dedup/event/ping topic.

let dedupReady = false;
cotonic.broker.subscribe("model/dedup/event/+key",
    function(msg, args) {
        switch(args.key)
        case "pong":
            dedupReady = true;
            break;
        ...
    });
=> When the dedup model is ready a pong message will be available

Edit on GitHub