@chainsafe/libp2p-yamux
Yamux stream multiplexer for libp2p
About
This module is a JavaScript implementation of Yamux from Hashicorp designed to be used with js-libp2p.
Example - Configure libp2p with Yamux
import { createLibp2p } from 'libp2p'
import { yamux } from '@chainsafe/libp2p-yamux'
const node = await createLibp2p({
streamMuxers: [
yamux()
]
})
Example - Using the low-level API
import { yamux } from '@chainsafe/libp2p-yamux'
import { pipe } from 'it-pipe'
import { duplexPair } from 'it-pair/duplex'
import all from 'it-all'
const clientMuxer = yamux({
client: true,
onIncomingStream: stream => {
pipe(stream, stream)
},
onStreamEnd: stream => {
}
})()
const serverMuxer = yamux({
client: false,
onIncomingStream: stream => {
pipe(stream, stream)
},
onStreamEnd: stream => {
}
})()
const p = duplexPair()
pipe(p[0], clientMuxer, p[0])
pipe(p[1], serverMuxer, p[1])
const stream0 = clientMuxer.newStream()
const stream1 = serverMuxer.newStream()
const encoder = new TextEncoder()
const data = [encoder.encode('hello'), encoder.encode('world')]
pipe(data, stream0)
const result = await pipe(stream0, all)
stream1.close()
clientMuxer.close()
Install
$ npm i @chainsafe/libp2p-yamux
Browser <script>
tag
Loading this module through a script tag will make its exports available as ChainsafeLibp2pYamux
in the global namespace.
<script src="https://unpkg.com/@chainsafe/libp2p-yamux/dist/index.min.js"></script>
API Docs
License
Licensed under either of
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.