Skip to content
This repository has been archived by the owner on Jul 21, 2023. It is now read-only.

Commit

Permalink
fix(docs): Update readme.md's example and added files for it
Browse files Browse the repository at this point in the history
  • Loading branch information
victorb committed Jan 20, 2017
1 parent d5e5ffb commit ccd94c8
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 45 deletions.
108 changes: 63 additions & 45 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -111,5 +129,5 @@ Emitted when the stream produces an error.
## Install

```sh
> npm i libp2p-multiplex
> npm install libp2p-multiplex
```
21 changes: 21 additions & 0 deletions examples/dialer.js
Original file line number Diff line number Diff line change
@@ -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
)
27 changes: 27 additions & 0 deletions examples/listener.js
Original file line number Diff line number Diff line change
@@ -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')
})

0 comments on commit ccd94c8

Please sign in to comment.