js-libp2p-tcp
Node.js implementation of the TCP module that libp2p uses, which implements the interface-connection interface for dial/listen.
Description
libp2p-tcp
in Node.js is a very thin shim that adds support for dialing to a multiaddr
. This small shim will enable libp2p to use other different transports.
Note: This module uses pull-streams for all stream based interfaces.
Example
const TCP = require('libp2p-tcp')
const multiaddr = require('multiaddr')
const pull = require('pull-stream')
const mh1 = multiaddr('/ip4/127.0.0.1/tcp/9090')
const mh2 = multiaddr('/ip6/::/tcp/9092')
const tcp = new TCP()
const listener = tcp.createListener(mh1, (socket) => {
console.log('new connection opened')
pull(
pull.values(['hello']),
socket
)
})
listener.listen(() => {
console.log('listening')
pull(
tcp.dial(mh1),
pull.log,
pull.onEnd(() => {
tcp.close()
})
)
})
outputs
listening
new connection opened
hello
Installation
npm
> npm i libp2p-tcp
This module uses pull-streams
We expose a streaming interface based on pull-streams
, rather then on the Node.js core streams implementation (aka Node.js streams). pull-streams
offers us a better mechanism for error handling and flow control guarantees. If you would like to know more about what took us to make this migration, see the discussion at this issue.
You can learn more about pull-streams at:
- The history of Node.js streams, nodebp April 2014
- The history of streams, 2016
- pull-streams, the simple streaming primitive
- pull-streams documentation
Converting pull-streams
to Node.js Streams
If you are a Node.js streams user, you can convert a pull-stream to Node.js Stream using the module pull-stream-to-stream
, giving you an instance of a Node.js stream that is linked to the pull-stream. Example:
const pullToStream = require('pull-stream-to-stream')
const nodeStreamInstance = pullToStream(pullStreamInstance)
// nodeStreamInstance is an instance of a Node.js Stream
To learn more about his utility, visit https://pull-stream.github.io/#pull-stream-to-stream
API
libp2p-tcp
accepts TCP addresses both IPFS and non IPFS encapsulated addresses, i.e:
/ip4/127.0.0.1/tcp/4001
/ip4/127.0.0.1/tcp/4001/ipfs/QmHash
Both for dialing and listening.
License
MIT