Skip to content

Commit 13cf92a

Browse files
committed
add orbit to embarkjs
1 parent 5dabf81 commit 13cf92a

File tree

6 files changed

+288
-8
lines changed

6 files changed

+288
-8
lines changed

README.md

Lines changed: 2 additions & 2 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,7 +351,7 @@ EmbarkJS - Communication
351351

352352
**initialization**
353353

354-
The current available communication is Whisper.
354+
The current available communications are Whisper and Orbit. Whisper is supported in go-ethereum. To run Orbit ```ipfs daemon --enable-pubsub-experiment```.
355355

356356
**listening to messages**
357357

js/build/embark.bundle.js

Lines changed: 81 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,20 +246,30 @@ 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+
} else if (provider === 'orbit') {
254+
this.currentMessages = EmbarkJS.Messages.Orbit;
255+
if (options === undefined) {
256+
ipfs = HaadIpfsApi('localhost', '5001');
257+
} else {
258+
ipfs = HaadIpfsApi(options.server, options.port);
259+
}
260+
this.currentMessages.orbit = new Orbit(ipfs);
261+
this.currentMessages.orbit.connect(web3.eth.accounts[0]);
252262
} else {
253263
throw Error('unknown provider');
254264
}
255265
};
256266

257267
EmbarkJS.Messages.sendMessage = function(options) {
258-
return EmbarkJS.Messages.Whisper.sendMessage(options);
268+
return this.currentMessages.sendMessage(options);
259269
};
260270

261271
EmbarkJS.Messages.listenTo = function(options) {
262-
return EmbarkJS.Messages.Whisper.listenTo(options);
272+
return this.currentMessages.listenTo(options);
263273
};
264274

265275
EmbarkJS.Messages.Whisper = {
@@ -349,6 +359,74 @@ var EmbarkJS =
349359
return promise;
350360
};
351361

362+
EmbarkJS.Messages.Orbit = {
363+
};
364+
365+
EmbarkJS.Messages.Orbit.sendMessage = function(options) {
366+
var topics = options.topic || options.topics;
367+
var data = options.data || options.payload;
368+
369+
if (topics === undefined) {
370+
throw new Error("missing option: topic");
371+
}
372+
373+
if (data === undefined) {
374+
throw new Error("missing option: data");
375+
}
376+
377+
// do fromAscii to each topics unless it's already a string
378+
if (typeof topics === 'string') {
379+
topics = topics;
380+
} else {
381+
// TODO: better to just send to different channels instead
382+
topics = topics.join(',');
383+
}
384+
385+
// TODO: if it's array then join and send ot several
386+
// keep list of channels joined
387+
this.orbit.join(topics);
388+
389+
var payload = JSON.stringify(data);
390+
391+
this.orbit.send(topics, data);
392+
};
393+
394+
EmbarkJS.Messages.Orbit.listenTo = function(options) {
395+
var self = this;
396+
var topics = options.topic || options.topics;
397+
398+
// do fromAscii to each topics unless it's already a string
399+
if (typeof topics === 'string') {
400+
topics = topics;
401+
} else {
402+
// TODO: better to just send to different channels instead
403+
topics = topics.join(',');
404+
}
405+
406+
var messageEvents = function() {
407+
this.cb = function() {};
408+
};
409+
410+
messageEvents.prototype.then = function(cb) {
411+
this.cb = cb;
412+
};
413+
414+
messageEvents.prototype.error = function(err) {
415+
return err;
416+
};
417+
418+
var promise = new messageEvents();
419+
420+
this.orbit.events.on('message', (topics, message) => {
421+
// Get the actual content of the message
422+
self.orbit.getPost(message.payload.value, true).then((post) => {
423+
promise.cb(post);
424+
});
425+
});
426+
427+
return promise;
428+
};
429+
352430
module.exports = EmbarkJS;
353431

354432

js/embark.js

Lines changed: 81 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,20 +199,30 @@ EmbarkJS.Storage.getUrl = function(hash) {
199199
EmbarkJS.Messages = {
200200
};
201201

202-
EmbarkJS.Messages.setProvider = function(provider) {
202+
EmbarkJS.Messages.setProvider = function(provider, options) {
203+
var ipfs;
203204
if (provider === 'whisper') {
204205
this.currentMessages = EmbarkJS.Messages.Whisper;
206+
} else if (provider === 'orbit') {
207+
this.currentMessages = EmbarkJS.Messages.Orbit;
208+
if (options === undefined) {
209+
ipfs = HaadIpfsApi('localhost', '5001');
210+
} else {
211+
ipfs = HaadIpfsApi(options.server, options.port);
212+
}
213+
this.currentMessages.orbit = new Orbit(ipfs);
214+
this.currentMessages.orbit.connect(web3.eth.accounts[0]);
205215
} else {
206216
throw Error('unknown provider');
207217
}
208218
};
209219

210220
EmbarkJS.Messages.sendMessage = function(options) {
211-
return EmbarkJS.Messages.Whisper.sendMessage(options);
221+
return this.currentMessages.sendMessage(options);
212222
};
213223

214224
EmbarkJS.Messages.listenTo = function(options) {
215-
return EmbarkJS.Messages.Whisper.listenTo(options);
225+
return this.currentMessages.listenTo(options);
216226
};
217227

218228
EmbarkJS.Messages.Whisper = {
@@ -302,4 +312,72 @@ EmbarkJS.Messages.Whisper.listenTo = function(options) {
302312
return promise;
303313
};
304314

315+
EmbarkJS.Messages.Orbit = {
316+
};
317+
318+
EmbarkJS.Messages.Orbit.sendMessage = function(options) {
319+
var topics = options.topic || options.topics;
320+
var data = options.data || options.payload;
321+
322+
if (topics === undefined) {
323+
throw new Error("missing option: topic");
324+
}
325+
326+
if (data === undefined) {
327+
throw new Error("missing option: data");
328+
}
329+
330+
// do fromAscii to each topics unless it's already a string
331+
if (typeof topics === 'string') {
332+
topics = topics;
333+
} else {
334+
// TODO: better to just send to different channels instead
335+
topics = topics.join(',');
336+
}
337+
338+
// TODO: if it's array then join and send ot several
339+
// keep list of channels joined
340+
this.orbit.join(topics);
341+
342+
var payload = JSON.stringify(data);
343+
344+
this.orbit.send(topics, data);
345+
};
346+
347+
EmbarkJS.Messages.Orbit.listenTo = function(options) {
348+
var self = this;
349+
var topics = options.topic || options.topics;
350+
351+
// do fromAscii to each topics unless it's already a string
352+
if (typeof topics === 'string') {
353+
topics = topics;
354+
} else {
355+
// TODO: better to just send to different channels instead
356+
topics = topics.join(',');
357+
}
358+
359+
var messageEvents = function() {
360+
this.cb = function() {};
361+
};
362+
363+
messageEvents.prototype.then = function(cb) {
364+
this.cb = cb;
365+
};
366+
367+
messageEvents.prototype.error = function(err) {
368+
return err;
369+
};
370+
371+
var promise = new messageEvents();
372+
373+
this.orbit.events.on('message', (topics, message) => {
374+
// Get the actual content of the message
375+
self.orbit.getPost(message.payload.value, true).then((post) => {
376+
promise.cb(post);
377+
});
378+
});
379+
380+
return promise;
381+
};
382+
305383
module.exports = EmbarkJS;

js/ipfs-api.min.js

Lines changed: 61 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/orbit.min.js

Lines changed: 60 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ Config.prototype.loadFiles = function(files) {
8484
if (file === 'embark.js') {
8585
readFiles.push({filename: 'web3.js', content: fs.readFileSync(path.join(__dirname, "/../js/web3.js")).toString()});
8686
readFiles.push({filename: 'ipfs.js', content: fs.readFileSync(path.join(__dirname, "/../js/ipfs.js")).toString()});
87+
// TODO: remove duplicated files if funcitonality is the same for storage and orbit
88+
readFiles.push({filename: 'ipfs-api.js', content: fs.readFileSync(path.join(__dirname, "/../js/ipfs-api.min.js")).toString()});
89+
readFiles.push({filename: 'orbit.js', content: fs.readFileSync(path.join(__dirname, "/../js/orbit.min.js")).toString()});
8790
readFiles.push({filename: 'embark.js', content: fs.readFileSync(path.join(__dirname, "/../js/build/embark.bundle.js")).toString()});
8891
} else {
8992
readFiles.push({filename: file, content: fs.readFileSync(file).toString()});

0 commit comments

Comments
 (0)