Holochain Forum

Mailbox

Problem

Agents need to send and receive messages when one party is offline, but node-to-node messaging only works when both agents are online.

Solution

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.

Implementation

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.

Warnings

  • 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.
2 Likes

NIce!

The link type will have to be to! because you can’t define an entry_def for the agent entry.

I’m still having issues getting links to an agents ID to propagate to other agents.

Do we have some suggestions / alternatives if the messages are large or plentiful?

Good point about not being able to define an agent ID entry type — should that be from! though? (As in, the message entry type has a link from! the agent ID entry type)

Thanks for the caution about agent ID links; I’ll put in an alternative.

I’ll also flesh out the alternatives for plentiful messages — along with how to avoid hotspots.

1 Like