Agents need to send and receive messages when one party is offline, but node-to-node messaging only works when both agents are online.
Write persistent messages to the DHT and link to them from an agent’s ‘mailbox’, an entry that they know to check when they come back online.
Every agent has a well-known entry on the DHT: their agent ID entry. Just like any other entry, you can use it as a base for a link.
Create an entry type for messages and a link type for mailbox notifications. The link should have agent ID entries as its base and message entries as its target. When an agent writes a message to the DHT, she creates a link from the recipient’s agent ID to that message. When the recipient comes online, he calls a zome function that retrieves all the mailbox notification links, retrieves the linked messages, and deletes the links.
- At time of writing (holochain v0.0.30-alpha6), there’s a bug that prevents links on an agent ID entry from being propagated through the DHT. Each agent should instead create an agent ID anchor in the
init()function and hang links from it instead. This anchor’s content can be a simple string containing their public key.
- This uses the DHT, so it creates a lot of persistent data. This might not be appropriate if messages are large or plentiful — in those cases, node-to-node messaging or a Throwaway DHT for the message content might be better.
- If the popularity of recipients follows a hockey-stick distribution curve, it will create ‘hot spots’ in the DHT neighbourhood of their agent ID addresses.
- Anyone can read messages on the DHT. If you want messages to be private, encrypt them with the recipient’s public key as in the Asynchronous Private Messaging pattern. But just be careful of the risks of that pattern.
- Curious people can find out who’s talking to whom by checking the authorship of links and messages. Don’t use this pattern if that is a problem for your use case.