Skip to content

Instantly share code, notes, and snippets.

@Raynos
Created November 20, 2012 07:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Raynos/4116515 to your computer and use it in GitHub Desktop.
Save Raynos/4116515 to your computer and use it in GitHub Desktop.
var WriteStream = require("write-stream")
, uuid = require("node-uuid")
, assert = require("assert")
// A pool of open peer connections. Used to connect to other people
, PeerConnectionPool = require("peer-connection-pool")
// A list of all peers in the network
, Peers = require("peer-nodes")
, peers = Peers()
, id = uuid()
// When a new peer connection opens you get a stream!
, pool = PeerConnectionPool(function (stream) {
// I am an echo stream
stream.pipe(stream)
}).listen(id)
// new peer in the network? Awesome connect to them!
peers.on("join", function (peer) {
var stream = pool.connect(peer.id)
stream.pipe(WriteStream(function onwrite(message) {
assert.equal(message, "hello world")
}))
stream.write("hello world")
})
// join the network!
peers.join({
id: id
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment