A simple wrapper to the mam.client for NodeJs

A simple wrapper to the mam.client for NodeJs

In order to simplify your work, we’ve created a small NodeJs (NPM) package that will help you implementing MAM messaging in your JavaScript project.

tangleMesh
Dec 14, 2019 · 4 min read

The IOTA Foundations has published a great new “second layer data communication protocol” for publishing messages with ensured integrity and security. This protocol runs on the decentralized tangle, so everyone has access to it and can use it for free. If you want to read more about this protocol, you should read the great article from the IOTA Foundation: Introducing masked Authenticated Messaging

We’ve used this new protocol introduced by the IOTA Foundation to enable messaging and queueing services to any developer for free or with additional functionality for a small fee. How this service works and what it is about can be read in our previous article. This article focuses on using the Message and Queue Service in a development environment.

For this purpose the IOTA Foundation has already published a neat package the mam.client.js. You can have a look on the code basis for developing NodeJs applications on their github account. We had a look on this package and liked the functionality of it and what possibilities it provides. But we have thought about how to make the use of MAM in companies or private projects even easier. That’s why we decided to write a simple and easy to use wrapper in NodeJs.

We have set ourselves the requirement that MAM messaging must be even easier to understand as a user and the application must look simpler, especially in existing projects. This wrapper uses the functions of the mam.client.js internally, but is based on an object-oriented approach and makes the use of the protocol even more modular.

A few new components have been introduced that allow a simple abstraction of the MAM construct: Provider, Stream and Message.

Provider is a component that provides different functions to publish and fetch messages from and to the tangle. Additionally it will handle the update of states which will become important later in this article. A provider could also be used to implement individual caching or even storage of states or messages and even more use-cases. Important to know is, that you can use our two predefined providers or you are able to create your own provider. We provide a native IOTA provider for using directly the tangle and also a tangleMesh:api provider which also uses the tangle but extends the default functionality with storing messages without the need of providing your own permanent node.

Stream as a component is responsible for sending and fetching messages through a selected provider. Basically this object is the representation of a so called MAM channel. So a stream can be initialized in one of the three different states: public, private or restricted. Correctly initialized it makes no difference in using this object. Therefore sending and publishing messages works exactly the same for private or restricted messages. The stream takes care of the encryption and decryption of messages so that these messages can be used uniformly in the application while being stored safely in the public tangle. A stream can be used on a theoretically unlimited number of clients. So there can be multiple senders and also unlimited receivers on each stream.

There are two different types of streams: Listener- and PublisherStreams. As the names suggest, the listener stream can only fetch and subscribe to messages inside this stream and the publisher stream can send but also receive messages of it’s stream. A stream consists of a specific state – which will handled by it’s own and the selected provider – and a chain of messages.

Message is the last important component which will be used inside our implementation of MAM messaging. This object contains the information and reference of one specific message of the tangle. So there can be an unlimited number of messages in a stream. This core component can be published and fetched from and to the tangle. They consist of a publisher, type, timestamp and the actual message. The type can be one of promotional and transactional. A promotional message is meant to stay only temporary, so this message will disappear on most nodes after doing a local snapshot. A transactional message should exist longer, for example keeping the history. In this case the provider is responsible for differentiating between the message type and storing the specific messages. In restricted mode a MAM message can also be encrypted. But this message object automatically decrypts the message when being received from the tangle. This way you can use the message the same way regardless of the mode of the stream.

If you like this approach you can read more about the functionality and how everything works in our NodeJs implementation message.js. The package is intended as a reference and can be converted into any other programming language. Feel free to try this package out and give some feedback!

Thank you for reading this article. If you are not yet familiar with our services, please visit our website https://tangle-mesh.io or follow us on Twitter. If you have further questions, we are available in our Discord or via our contact form!

tangleMesh

Written by

We use our energy and strength to ensure that everyone has easy access to the Tangle. We believe in the freedom and the possibilities of the Tangle.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade