Skip to content

Commit a8a97ce

Browse files
committed
Annotate code examples in README.md correctly
1 parent 0a7f3f1 commit a8a97ce

1 file changed

Lines changed: 91 additions & 75 deletions

File tree

README.md

Lines changed: 91 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -12,36 +12,38 @@ This is an npm-package and you can add it to your project by using npm with:
1212
## Usage
1313
Below you find a minimum usage example on how to use our message-library. First you need to import the required packages. Afterwards you can create a provider and a stream. With your created message-stream you can then send and fetch messages. Every message will be sent and fetched to and from the tangle, regardless of which provider you are using.
1414

15-
const { IOTAProvider, Stream, Mode, Message } = require ("@tanglemesh/message.js");
16-
const { ApiAuthentificator } = require ("@tanglemesh/api-client.js");
15+
```javascript
16+
const { IOTAProvider, Stream, Mode, Message } = require ("@tanglemesh/message.js");
17+
const { ApiAuthentificator } = require ("@tanglemesh/api-client.js");
1718

18-
(async () => {
19+
(async () => {
1920

20-
//Create a provider that should be used as middleware to the tangle
21-
const provider = new IOTAProvider ("https://nodes.thetangle.org:443");
21+
//Create a provider that should be used as middleware to the tangle
22+
const provider = new IOTAProvider ("https://nodes.thetangle.org:443");
2223

23-
//Create a new stream for publishing and receiving messages
24-
let stream = await Stream.createPublisherStream (
25-
provider, // your message provider that should be used for all actions made with this stream
26-
"XXXXOK9TYGDOBSAKRCGMDSDFVHOQXMM9HWLQZPHYAZBIPDMZWPXJLSRQDSPWULQGAWQWQFEQQ9CI9XAY9", // your seed
27-
);
24+
//Create a new stream for publishing and receiving messages
25+
let stream = await Stream.createPublisherStream (
26+
provider, // your message provider that should be used for all actions made with this stream
27+
"XXXXOK9TYGDOBSAKRCGMDSDFVHOQXMM9HWLQZPHYAZBIPDMZWPXJLSRQDSPWULQGAWQWQFEQQ9CI9XAY9", // your seed
28+
);
2829

29-
//Create a new Message to publish
30-
const message = Message.createMessage (
31-
stream,
32-
"some example message"
33-
);
30+
//Create a new Message to publish
31+
const message = Message.createMessage (
32+
stream,
33+
"some example message"
34+
);
3435

35-
//Publish the previously created message
36-
const publishedMessages = await stream.sendMessages (message);
36+
//Publish the previously created message
37+
const publishedMessages = await stream.sendMessages (message);
3738

38-
//Fetch all new messages and output them
39-
const fetchedMessages = await stream.fetchMessages ();
40-
for (const fetchedMessage of fetchedMessages) {
41-
console.log (fetchedMessage.Message, fetchedMessage.Type, fetchedMessage.Publisher, fetchedMessage.Timestamp);
42-
}
39+
//Fetch all new messages and output them
40+
const fetchedMessages = await stream.fetchMessages ();
41+
for (const fetchedMessage of fetchedMessages) {
42+
console.log (fetchedMessage.Message, fetchedMessage.Type, fetchedMessage.Publisher, fetchedMessage.Timestamp);
43+
}
4344

44-
})();
45+
})();
46+
```
4547

4648
## Basic explanations
4749
This packages contains some different classes that you need to know, to use it properly.
@@ -54,9 +56,11 @@ One importent component of this package are the providers. In this version there
5456

5557
You can use both of these providers to send and fetch messages through the tangle. If you'd like to create your own provider, you can simply create your own provider class by extending the class `provider`.
5658

57-
const { Provider } = require ("@tanglemesh/message.js");
59+
```javascript
60+
const { Provider } = require ("@tanglemesh/message.js");
5861

59-
class OwnProvider extends Provider {…}
62+
class OwnProvider extends Provider {…}
63+
```
6064

6165
To create a working own provider, you can have a look into our provided providers that you can find in [./src/providers](./src/providers).
6266
But normally the two providers mentioned above should work for you in most cases.
@@ -72,34 +76,38 @@ A Provider basically has four different methods, that you can use:
7276

7377
This is everything a provider does for you. And even better: in most use cases you do not even have to call this messages on your own, because the stream will do that for you. You only have to initialize the providers as follows:
7478

75-
//Using the IOTAProvider
76-
const { IOTAProvider } = require ("@tanglemesh/message.js");
79+
```javascript
80+
//Using the IOTAProvider
81+
const { IOTAProvider } = require ("@tanglemesh/message.js");
7782

78-
const iotaProvider = new IOTAProvider ("https://node02.iotatoken.nl:443");
83+
const iotaProvider = new IOTAProvider ("https://node02.iotatoken.nl:443");
7984

8085

81-
//Using the TMProvider
82-
const { TMProvider } = require ("@tanglemesh/message.js");
83-
const { ApiAuthentificator } = require ("@tanglemesh/api-client.js");
86+
//Using the TMProvider
87+
const { TMProvider } = require ("@tanglemesh/message.js");
88+
const { ApiAuthentificator } = require ("@tanglemesh/api-client.js");
8489

85-
// Set credentials for using the tangleMesh:api
86-
const apiIdentifier = "315F5B4E62DBFA7A2DA57D88FAA016A9",
87-
apiSecret = "49535045477F2EBDE0C3D05C1556A3925C5A6A2B93D2255A7DBF6F8EEC96AB1D",
88-
channelId = "8ef56408-37fd-4024-bc11-36e00cb9ede3";
89-
const apiAuth = new ApiAuthentificator (apiIdentifier, apiSecret);
90+
// Set credentials for using the tangleMesh:api
91+
const apiIdentifier = "315F5B4E62DBFA7A2DA57D88FAA016A9",
92+
apiSecret = "49535045477F2EBDE0C3D05C1556A3925C5A6A2B93D2255A7DBF6F8EEC96AB1D",
93+
channelId = "8ef56408-37fd-4024-bc11-36e00cb9ede3";
94+
const apiAuth = new ApiAuthentificator (apiIdentifier, apiSecret);
9095

91-
const tmProvider = new TMProvider ("https://node02.iotatoken.nl:443", apiAuth, "channelid");
96+
const tmProvider = new TMProvider ("https://node02.iotatoken.nl:443", apiAuth, "channelid");
97+
```
9298

9399
The most important parameter that every provider has is the `fullnode-url` that you can set to any fullnode you have access to.
94100

95101
### Mode
96102
A second small building block in this package is the `Mode` class. In MAM messaging there are three different modes for sending and receiving messages. This class simply represents the three different methods:
97103

98-
const { Mode } = require ("@tanglemesh/message.js");
104+
```javascript
105+
const { Mode } = require ("@tanglemesh/message.js");
99106

100-
const publicMode = Mode.PUBLIC;
101-
const privateMode = Mode.PRIVATE;
102-
const restrictedMode = Mode.RESTRICTED;
107+
const publicMode = Mode.PUBLIC;
108+
const privateMode = Mode.PRIVATE;
109+
const restrictedMode = Mode.RESTRICTED;
110+
```
103111

104112
### Stream
105113
A stream is the core component of this package. A stream simply is a specified channel of MAM messages on the tangle. You can use a stream to publish and receive messages. There are two different types of streams:
@@ -111,39 +119,45 @@ Like the names suggest is the PublisherStream for sending **and** receiving mess
111119

112120
For a ListenerStream you only need the `provider` (fullnode api-url), one of the above `mode`s, the `root` of the stream and optionally a `sideKey` for:
113121

114-
const { Stream } = require ("@tanglemesh/message.js");
122+
```javascript
123+
const { Stream } = require ("@tanglemesh/message.js");
115124

116-
const stream = await Stream.createListenerStream (
117-
provider, // your message provider that should be used for all actions made with this stream
118-
root,
119-
mode,
120-
security,
121-
sideKey
122-
);
125+
const stream = await Stream.createListenerStream (
126+
provider, // your message provider that should be used for all actions made with this stream
127+
root,
128+
mode,
129+
security,
130+
sideKey
131+
);
132+
```
123133

124134
To create a new PublisherStream you have to call the method `Stream.createPublisherStream` with you `provider` (fullnode api-url), your `seed` (will only stored locally on your device regardless of the provider!), the `mode` and optionally the `sideKey` for encrypting and decrypting messages.
125135

126-
const { Stream } = require ("@tanglemesh/message.js");
136+
```javascript
137+
const { Stream } = require ("@tanglemesh/message.js");
127138

128-
const stream = await Stream.createPublisherStream (
129-
provider, // your message provider that should be used for all actions made with this stream
130-
seed,
131-
mode,
132-
security,
133-
sideKey
134-
);
139+
const stream = await Stream.createPublisherStream (
140+
provider, // your message provider that should be used for all actions made with this stream
141+
seed,
142+
mode,
143+
security,
144+
sideKey
145+
);
146+
```
135147

136148
If you have already create a PublisherStream and you want to initialize the stream with an up to date state, you have to call the method `Stream.initializePublisherStream`. With this method you should use the same properties that you used previously with the above method.
137149

138-
const { Stream } = require ("@tanglemesh/message.js");
150+
```javascript
151+
const { Stream } = require ("@tanglemesh/message.js");
139152

140-
const stream = await Stream.initializePublisherStream (
141-
provider, // your message provider that should be used for all actions made with this stream
142-
seed,
143-
mode,
144-
security,
145-
sideKey
146-
);
153+
const stream = await Stream.initializePublisherStream (
154+
provider, // your message provider that should be used for all actions made with this stream
155+
seed,
156+
mode,
157+
security,
158+
sideKey
159+
);
160+
```
147161

148162
All these three methods will create a `stream`-object for you, that you can now use for further actions:
149163

@@ -160,16 +174,18 @@ The [mam.client.js](https://github.com/iotaledger/mam.client.js) from the iota f
160174
### Messages
161175
The last but not least component is the `Message` class. Each MAM message is represented by a own message object. You can create own messages like in the following example:
162176

163-
const { Message } = require ("@tanglemesh/message.js");
164-
165-
const message = Message.createMessage (
166-
stream,
167-
{
168-
"some": "example",
169-
"test": 1.3
170-
},
171-
Message.TYPE_PROMOTIONAL
172-
);
177+
```javascript
178+
const { Message } = require ("@tanglemesh/message.js");
179+
180+
const message = Message.createMessage (
181+
stream,
182+
{
183+
"some": "example",
184+
"test": 1.3
185+
},
186+
Message.TYPE_PROMOTIONAL
187+
);
188+
```
173189

174190
You get the same message object by fetching messages with your stream. A Message has always a specific type:
175191

0 commit comments

Comments
 (0)