Dipping our toes into MAM

Let’s learn about IOTA’s MAM Module together

As I’ve been following along with IOTA for the last year or so, I’ve been working on and off of a few projects here and there. Of late, I’ve been wanting to get some experience working with MAM and Flash Channels. Since I couldn’t find too many guides out there, I figured why not write one? So this is a learning experience for both of us. If you see flaws in my code or logic etc, please let me know — I don’t claim to be an expert, just another guy trying to figure out how to build things with this new tech.

Ok. Preamble over. Onto the code.

For part 1 of this guide into Masked Authenticated Messaging, I wanted to start with an idea I’ve been thinking about for some time, but haven’t got around to building — a simple pub/sub server and client for IOTA.

Here’s how it works. There is a publisher, which publishes messages to the tangle, and a subscriber which listens and responds to those messages. Seems pretty simple right? Well it is.

Setting things up

I started with a blank repo, but you can start from my code here. I’m also using the mam.client.js library written by the other Lewis from Australia in IOTA (well I don’t actually work for the IOTA foundation, but I think it’s funny that their resident Aussie is also named Lewis).

publisher/src/app.ts

Here we have a simple Express server which listens for a message. When it receives this message, it publishes a message to MAM, and returns the root of the channel.

We can run this by doing something along the following lines:

$ yarn #install dependencies
$ yarn run watch #run the ts compiler, and actually run the app
Publisher listening on port 3000!

Next, we can pretty easily publish a message to MAM with the following:

$ curl "localhost:3000?message=HILEWIS"                                               

Which may take a little while (depending if you’re on the test net, or public, or your own node etc.) and will give you a response such as the following.

{"message":"HILEWIS","root":"DAEU9MEXGTVPZFRFUBGPWTDIVJR9FNLSIJZ9IAU9JWNYCQIKYIBJKSVGALPAXINYFJFM9NOQNTYOOTKY9"}

Great! So we’re publishing messages to the Tangle with MAM. So far so good. Let’s move on to the subscriber. Don’t worry, it’s about as easy.

Here’s what I have for my subscriber:

Here we have the subscriber, which takes the root given to us from the output of the first message. We set it using a simple environment variable.

The subscriber looks for a message, and when it finds the message, it recursively calls Mam.fetch on the next root. Since the next root doesn’t change when there are no new messages, we can just keep calling this recursively until we get a new message.

And here’s how we run it (make sure to replace the root with the root from your first message).

$ export ROOT='DAEU9MEXGTVPZFRFUBGPWTDIVJR9FNLSIJZ9IAU9JWNYCQIKYIBJKSVGALPAXINYFJFM9NOQNTYOOTKY9'
$ yarn run watch

And that’s pretty much it. Keep sending messages using the curl command, and you will see them come up in the subscriber. You will also notice that the root changes each time. That’s the merkle tree working it’s magic behind the scenes.

This is some simple and kinda nasty programming, but it demonstrates a simple pub/sub with MAM.

Next up, I think we’re going to look at applying some security to this, to make sure nobody looks in on our precious messages.

If you enjoyed this guide, or have any suggestions or questions, let me know in the comments. If you liked this post, give it a ❤️ or a 👏, or whatever you crazy cats are calling it nowadays.