Skip to content

Commit 04bb61b

Browse files
committed
Merge branch 'orbit' into develop
2 parents 59c63b9 + a8beb1b commit 04bb61b

File tree

6 files changed

+343
-27
lines changed

6 files changed

+343
-27
lines changed

README.md

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Table of Contents
2828
* [Using and Configuring Contracts](#dapp-structure)
2929
* [EmbarkJS](#embarkjs)
3030
* [EmbarkJS - Storage (IPFS)](#embarkjs---storage)
31-
* [EmbarkJS - Communication (Whisper)](#embarkjs---communication)
31+
* [EmbarkJS - Communication (Whisper/Orbit)](#embarkjs---communication)
3232
* [Testing Contracts](#tests)
3333
* [Working with different chains](#working-with-different-chains)
3434
* [Custom Application Structure](#structuring-application)
@@ -351,28 +351,40 @@ EmbarkJS - Communication
351351

352352
**initialization**
353353

354-
The current available communication is Whisper.
354+
For Whisper:
355+
356+
```EmbarkJS.Messages.setProvider('whisper')```
357+
358+
For Orbit:
359+
360+
You'll need to use IPFS from master and run it as: ```ipfs daemon --enable-pubsub-experiment```.
361+
362+
then set the provider:
363+
364+
```EmbarkJS.Messages.setProvider('orbit', {server: 'localhost', port: 5001})```
355365

356366
**listening to messages**
357367

358368
```Javascript
359-
EmbarkJS.Messages.listenTo({topic: ["achannel", "anotherchannel"]}).then(function(message) { console.log("received: " + message); })
369+
EmbarkJS.Messages.listenTo({topic: ["topic1", "topic2"]}).then(function(message) { console.log("received: " + message); })
360370
```
361371

362372
**sending messages**
363373

364374
you can send plain text
365375

366376
```Javascript
367-
EmbarkJS.Messages.sendMessage({topic: "achannel", data: 'hello world'})
377+
EmbarkJS.Messages.sendMessage({topic: "sometopic", data: 'hello world'})
368378
```
369379

370380
or an object
371381

372382
```Javascript
373-
EmbarkJS.Messages.sendMessage({topic: "achannel", data: {msg: 'hello world'}})
383+
EmbarkJS.Messages.sendMessage({topic: "sometopic", data: {msg: 'hello world'}})
374384
```
375385

386+
note: array of topics are considered an AND. In Whisper you can use another array for OR combinations of several topics e.g ```["topic1", ["topic2", "topic3"]]``` => ```topic1 AND (topic2 OR topic 3)```
387+
376388
Tests
377389
======
378390

js/build/embark.bundle.js

Lines changed: 101 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -246,20 +246,31 @@ var EmbarkJS =
246246
EmbarkJS.Messages = {
247247
};
248248

249-
EmbarkJS.Messages.setProvider = function(provider) {
249+
EmbarkJS.Messages.setProvider = function(provider, options) {
250+
var ipfs;
250251
if (provider === 'whisper') {
251252
this.currentMessages = EmbarkJS.Messages.Whisper;
253+
this.currentMessages.identity = web3.shh.newIdentity();
254+
} else if (provider === 'orbit') {
255+
this.currentMessages = EmbarkJS.Messages.Orbit;
256+
if (options === undefined) {
257+
ipfs = HaadIpfsApi('localhost', '5001');
258+
} else {
259+
ipfs = HaadIpfsApi(options.server, options.port);
260+
}
261+
this.currentMessages.orbit = new Orbit(ipfs);
262+
this.currentMessages.orbit.connect(web3.eth.accounts[0]);
252263
} else {
253264
throw Error('unknown provider');
254265
}
255266
};
256267

257268
EmbarkJS.Messages.sendMessage = function(options) {
258-
return EmbarkJS.Messages.Whisper.sendMessage(options);
269+
return this.currentMessages.sendMessage(options);
259270
};
260271

261272
EmbarkJS.Messages.listenTo = function(options) {
262-
return EmbarkJS.Messages.Whisper.listenTo(options);
273+
return this.currentMessages.listenTo(options);
263274
};
264275

265276
EmbarkJS.Messages.Whisper = {
@@ -268,7 +279,7 @@ var EmbarkJS =
268279
EmbarkJS.Messages.Whisper.sendMessage = function(options) {
269280
var topics = options.topic || options.topics;
270281
var data = options.data || options.payload;
271-
var identity = options.identity || web3.shh.newIdentity();
282+
var identity = options.identity || this.identity || web3.shh.newIdentity();
272283
var ttl = options.ttl || 100;
273284
var priority = options.priority || 1000;
274285

@@ -282,21 +293,21 @@ var EmbarkJS =
282293

283294
// do fromAscii to each topics unless it's already a string
284295
if (typeof topics === 'string') {
285-
topics = topics;
296+
_topics = [web3.fromAscii(topics)];
286297
} else {
287298
// TODO: replace with es6 + babel;
288299
var _topics = [];
289300
for (var i = 0; i < topics.length; i++) {
290301
_topics.push(web3.fromAscii(topics[i]));
291302
}
292-
topics = _topics;
293303
}
304+
topics = _topics;
294305

295306
var payload = JSON.stringify(data);
296307

297308
var message = {
298309
from: identity,
299-
topics: [web3.fromAscii(topics)],
310+
topics: topics,
300311
payload: web3.fromAscii(payload),
301312
ttl: ttl,
302313
priority: priority
@@ -309,15 +320,15 @@ var EmbarkJS =
309320
var topics = options.topic || options.topics;
310321

311322
if (typeof topics === 'string') {
312-
topics = [topics];
323+
_topics = [topics];
313324
} else {
314325
// TODO: replace with es6 + babel;
315326
var _topics = [];
316327
for (var i = 0; i < topics.length; i++) {
317-
_topics.push(web3.fromAscii(topics[i]));
328+
_topics.push(topics[i]);
318329
}
319-
topics = _topics;
320330
}
331+
topics = _topics;
321332

322333
var filterOptions = {
323334
topics: topics
@@ -339,16 +350,95 @@ var EmbarkJS =
339350

340351
var filter = web3.shh.filter(filterOptions, function(err, result) {
341352
var payload = JSON.parse(web3.toAscii(result.payload));
353+
var data;
342354
if (err) {
343355
promise.error(err);
344356
} else {
345-
promise.cb(payload);
357+
data = {
358+
topic: topics,
359+
data: payload,
360+
from: result.from,
361+
time: (new Date(result.sent * 1000))
362+
};
363+
promise.cb(payload, data, result);
346364
}
347365
});
348366

349367
return promise;
350368
};
351369

370+
EmbarkJS.Messages.Orbit = {
371+
};
372+
373+
EmbarkJS.Messages.Orbit.sendMessage = function(options) {
374+
var topics = options.topic || options.topics;
375+
var data = options.data || options.payload;
376+
377+
if (topics === undefined) {
378+
throw new Error("missing option: topic");
379+
}
380+
381+
if (data === undefined) {
382+
throw new Error("missing option: data");
383+
}
384+
385+
if (typeof topics === 'string') {
386+
topics = topics;
387+
} else {
388+
// TODO: better to just send to different channels instead
389+
topics = topics.join(',');
390+
}
391+
392+
this.orbit.join(topics);
393+
394+
var payload = JSON.stringify(data);
395+
396+
this.orbit.send(topics, data);
397+
};
398+
399+
EmbarkJS.Messages.Orbit.listenTo = function(options) {
400+
var self = this;
401+
var topics = options.topic || options.topics;
402+
403+
if (typeof topics === 'string') {
404+
topics = topics;
405+
} else {
406+
topics = topics.join(',');
407+
}
408+
409+
this.orbit.join(topics);
410+
411+
var messageEvents = function() {
412+
this.cb = function() {};
413+
};
414+
415+
messageEvents.prototype.then = function(cb) {
416+
this.cb = cb;
417+
};
418+
419+
messageEvents.prototype.error = function(err) {
420+
return err;
421+
};
422+
423+
var promise = new messageEvents();
424+
425+
this.orbit.events.on('message', (channel, message) => {
426+
// TODO: looks like sometimes it's receving messages from all topics
427+
if (topics !== channel) return;
428+
self.orbit.getPost(message.payload.value, true).then((post) => {
429+
var data = {
430+
topic: channel,
431+
data: post.content,
432+
from: post.meta.from.name,
433+
time: (new Date(post.meta.ts))
434+
};
435+
promise.cb(post.content, data, post);
436+
});
437+
});
438+
439+
return promise;
440+
};
441+
352442
module.exports = EmbarkJS;
353443

354444

0 commit comments

Comments
 (0)