diff --git a/README.md b/README.md index 111fba9..9fab2c8 100644 --- a/README.md +++ b/README.md @@ -14,71 +14,89 @@ js-libp2p-multiplex ## Usage -Let's define `server.js`, which runs two services over a single TCP socket -running port 9000: +Let's define a `listener.js`, which starts a TCP server on port 9999 +and waits for a connection. Once we get a connection, we wait for +a stream. And finally, once we have the stream, we pull the data +from that stream, and printing it to the console. ```JavaScript -var multiplex = require('libp2p-multiplex') -var net = require('net') - -var server = net.createServer(function (conn) { - var multi = multiplex(conn, false) - - multi.newStream(echoService) - multi.newStream(lengthService) -}).listen(9000) - - -// Repeat back messages verbatim. -function echoService (err, conn) { - if (err) throw err - conn.on('data', function (data) { - conn.write(data) +const multiplex = require('libp2p-multiplex') +const tcp = require('net') +const pull = require('pull-stream') +const toPull = require('stream-to-pull-stream') + +const listener = tcp.createServer((socket) => { + console.log('[listener] Got connection!') + + const muxer = multiplex.listener(toPull(socket)) + + muxer.on('stream', (stream) => { + console.log('[listener] Got stream!') + pull( + stream, + pull.drain((data) => { + console.log('[listener] Received:') + console.log(data.toString()) + }) + ) }) -} +}) -// Respond with the length of each message. -function lengthService (err, conn) { - if (err) throw err - conn.on('data', function (data) { - conn.write(data.length+'\n') - }) -} +listener.listen(9999, () => { + console.log('[listener] listening on 9999') +}) ``` -Let's also define `client.js`, multiplexed over a TCP socket that writes to both -services: +Now, let's define `dialer.js` who will connect to our `listener` over a TCP socket. Once we have that, we'll put a message in the stream for our `listener`. ```JavaScript -var multiplex = require('libp2p-multiplex') -var net = require('net') +const multiplex = require('libp2p-multiplex') +const tcp = require('net') +const pull = require('pull-stream') +const toPull = require('stream-to-pull-stream') -var client = net.connect(9000, 'localhost', function () { - var multi = multiplex(client, true) +const socket = tcp.connect(9999) - multi.on('stream', function (conn) { - console.log('got a new stream') +const muxer = multiplex.dialer(toPull(socket)) - conn.on('data', function (data) { - console.log('message', data.toString()) - }) - }) +console.log('[dialer] opening stream') +const stream = muxer.newStream((err) => { + console.log('[dialer] opened stream') + if (err) throw err }) + +pull( + pull.values(['hey, how is it going. I am the dialer']), + stream +) +``` + +Now we can first run `listener.js` and then `dialer.js` to see the +following output: + +*listener.js* + +``` +$ node listener.js +[listener] listening on 9999 +[listener] Got connection! +[listener] Got stream! +[listener] Received: +hey, how is it going. I am the dialer ``` -Run both and see the output on the client: +*dialer.js* ``` -got a new stream -got a new stream -message hello there! -message 12 +$ node dialer.js +[dialer] opening stream +[dialer] opened stream ``` ## API ```js -var multiplex = require('libp2p-multiplex') +const multiplex = require('libp2p-multiplex') ``` #### var multi = multiplex(transport, isListener) @@ -111,5 +129,5 @@ Emitted when the stream produces an error. ## Install ```sh -> npm i libp2p-multiplex +> npm install libp2p-multiplex ``` diff --git a/examples/dialer.js b/examples/dialer.js new file mode 100644 index 0000000..5b8973c --- /dev/null +++ b/examples/dialer.js @@ -0,0 +1,21 @@ +'use strict' + +const tcp = require('net') +const pull = require('pull-stream') +const toPull = require('stream-to-pull-stream') +const multiplex = require('../src') + +const socket = tcp.connect(9999) + +const muxer = multiplex.dialer(toPull(socket)) + +console.log('[dialer] opening stream') +const stream = muxer.newStream((err) => { + console.log('[dialer] opened stream') + if (err) throw err +}) + +pull( + pull.values(['hey, how is it going. I am the dialer']), + stream +) diff --git a/examples/listener.js b/examples/listener.js new file mode 100644 index 0000000..d8080ba --- /dev/null +++ b/examples/listener.js @@ -0,0 +1,27 @@ +'use strict' + +const tcp = require('net') +const pull = require('pull-stream') +const toPull = require('stream-to-pull-stream') +const multiplex = require('../src') + +const listener = tcp.createServer((socket) => { + console.log('[listener] Got connection!') + + const muxer = multiplex.listener(toPull(socket)) + + muxer.on('stream', (stream) => { + console.log('[listener] Got stream!') + pull( + stream, + pull.drain((data) => { + console.log('[listener] Received:') + console.log(data.toString()) + }) + ) + }) +}) + +listener.listen(9999, () => { + console.log('[listener] listening on 9999') +})