When publishing a single message with REST using the publish request, it is possible to specify the id of a Message as part of idempotency. This will mean that any attempt to publish a Message with the same id will result in Ably not sending the presumably duplicated message.
Sometimes it is desirable to be able to include multiple messages in a single publish request. When multiple messages are included in a single publish request, those messages are published together by Ably as a unit. From the point of view of enabling idempotency, this unit corresponds to a single id. This means that it's not possible to include different, unrelated, ids in the messages when publishing them if they are all combined into a single REST publish request body.
Given this, there are two ways to make use of idempotency when publishing a group of messages together:
- If you will be reattempting to publish a group of messages in the same order as a prior publish, it is possible to choose a single base id for the set, and ensure that each of the individual message ids derive from this base id. Each message needs to contain an id of the form <base id>:<idx> where idx is a zero-based index into the array of messages. So if, for example, you want to publish 3 messages with a base id of foo, then the messages would have ids foo:0, foo:1, foo:2.
- It is possible that, by the time the reattempt is made, one of the message might have become superseded, and the application wants to avoid re-sending stale messages. In this case it is only possible to achieve the dual goals of (i) using a single REST request and (ii) idempotency by using the beta batch publishing API. This API can be used to send multiple messages, to the same or different channels, but allows those messages to be processed as independent operations. Using this, each message for a given channel can be assigned its own id.
Please get in touch for any further help or questions you may have on this.