Skip to content

Commit

Permalink
Merge pull request #3 from diasdavid/example/using-swarm
Browse files Browse the repository at this point in the history
using swarm example
  • Loading branch information
daviddias committed Dec 15, 2015
2 parents d79abcc + c4b6de1 commit 815a85d
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 1 deletion.
4 changes: 4 additions & 0 deletions examples/level1-swarm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Using libp2p-swarm
==================


37 changes: 37 additions & 0 deletions examples/level1-swarm/node-1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
var Swarm = require('libp2p-swarm')
var tcp = require('libp2p-tcp')
var multiaddr = require('multiaddr')
var Id = require('peer-id')
var Spdy = require('libp2p-spdy')
var Libp2p = require('../../src')
var Peer = require('peer-info')

// set up

var mh = multiaddr('/ip4/127.0.0.1/tcp/8010')
var p = new Peer(Id.create(), [])
var sw = new Swarm(p)

// create a libp2p node

var node = new Libp2p(sw)

node.swarm.addTransport('tcp', tcp, {multiaddr: mh}, {}, {port: 8010}, function () {
// Ready to receive incoming connections

sw.addStreamMuxer('spdy', Spdy, {})

// dial to another node

var mh2 = multiaddr('/ip4/127.0.0.1/tcp/8020')
var p2 = new Peer(Id.create(), [mh2])

node.swarm.dial(p2, {}, '/sparkles/1.0.0', function (err, conn) {
if (err) {
return console.error(err)
}

console.log('-> connection is ready')
process.stdin.pipe(conn).pipe(process.stdout)
})
})
29 changes: 29 additions & 0 deletions examples/level1-swarm/node-2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
var Swarm = require('libp2p-swarm')
var tcp = require('libp2p-tcp')
var multiaddr = require('multiaddr')
var Id = require('peer-id')
var Spdy = require('libp2p-spdy')
var Libp2p = require('../../src')
var Peer = require('peer-info')

// set up

var mh = multiaddr('/ip4/127.0.0.1/tcp/8020')
var p = new Peer(Id.create(), [])
var sw = new Swarm(p)

sw.addTransport('tcp', tcp, {multiaddr: mh}, {}, {port: 8020}, function () {
// Ready to receive incoming connections

sw.addStreamMuxer('spdy', Spdy, {})

// create a libp2p node

var node = new Libp2p(sw)

// handle/mount a protocol

node.swarm.handleProtocol('/sparkles/1.0.0', function (conn) {
process.stdin.pipe(conn).pipe(process.stdout)
})
})
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
"devDependencies": {
"code": "^1.5.0",
"lab": "^5.17.0",
"libp2p-spdy": "^0.1.0",
"libp2p-swarm": "^0.5.5",
"libp2p-tcp": "^0.1.2",
"multiaddr": "^1.1.1",
"peer-id": "^0.4.0",
"peer-info": "^0.4.0",
"pre-commit": "^1.1.1",
"standard": "^5.3.1"
}
Expand Down
1 change: 0 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

exports = module.exports = Libp2p

function Libp2p (swarm, peerRouting, recordStore) {
Expand Down

0 comments on commit 815a85d

Please sign in to comment.