https://github.com/chainsafe/js-libp2p-yamux
Typescript implementation of Yamux
https://github.com/chainsafe/js-libp2p-yamux
javascript libp2p libp2p-muxer stream typescript
Last synced: 5 months ago
JSON representation
Typescript implementation of Yamux
- Host: GitHub
- URL: https://github.com/chainsafe/js-libp2p-yamux
- Owner: ChainSafe
- License: apache-2.0
- Created: 2022-03-15T00:51:16.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2025-06-25T13:17:05.000Z (7 months ago)
- Last Synced: 2025-08-18T13:50:36.640Z (5 months ago)
- Topics: javascript, libp2p, libp2p-muxer, stream, typescript
- Language: TypeScript
- Homepage:
- Size: 821 KB
- Stars: 16
- Watchers: 11
- Forks: 13
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE-APACHE
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# @chainsafe/libp2p-yamux
[](https://codecov.io/gh/ChainSafe/js-libp2p-yamux)
[](https://github.com/ChainSafe/js-libp2p-yamux/actions/workflows/js-test-and-release.yml?query=branch%3Amaster)
> Yamux stream multiplexer for libp2p
# About
This module is a JavaScript implementation of [Yamux from Hashicorp](https://github.com/hashicorp/yamux/blob/master/spec.md) designed to be used with [js-libp2p](https://github.com/libp2p/js-libp2p).
## Example - Configure libp2p with Yamux
```typescript
import { createLibp2p } from 'libp2p'
import { yamux } from '@chainsafe/libp2p-yamux'
const node = await createLibp2p({
// ... other options
streamMuxers: [
yamux()
]
})
```
## Example - Using the low-level API
```js
import { yamux } from '@chainsafe/libp2p-yamux'
import { pipe } from 'it-pipe'
import { duplexPair } from 'it-pair/duplex'
import all from 'it-all'
// Connect two yamux muxers to demo basic stream multiplexing functionality
const clientMuxer = yamux({
client: true,
onIncomingStream: stream => {
// echo data on incoming streams
pipe(stream, stream)
},
onStreamEnd: stream => {
// do nothing
}
})()
const serverMuxer = yamux({
client: false,
onIncomingStream: stream => {
// echo data on incoming streams
pipe(stream, stream)
},
onStreamEnd: stream => {
// do nothing
}
})()
// `p` is our "connections", what we use to connect the two sides
// In a real application, a connection is usually to a remote computer
const p = duplexPair()
// connect the muxers together
pipe(p[0], clientMuxer, p[0])
pipe(p[1], serverMuxer, p[1])
// now either side can open streams
const stream0 = clientMuxer.newStream()
const stream1 = serverMuxer.newStream()
// Send some data to the other side
const encoder = new TextEncoder()
const data = [encoder.encode('hello'), encoder.encode('world')]
pipe(data, stream0)
// Receive data back
const result = await pipe(stream0, all)
// close a stream
stream1.close()
// close the muxer
clientMuxer.close()
```
# Install
```console
$ npm i @chainsafe/libp2p-yamux
```
## Browser `` tag
Loading this module through a script tag will make its exports available as `ChainsafeLibp2pYamux` in the global namespace.
```html
<script src="https://unpkg.com/@chainsafe/libp2p-yamux/dist/index.min.js">
```
# API Docs
-
# License
Licensed under either of
- Apache 2.0, ([LICENSE-APACHE](https://github.com/ChainSafe/js-libp2p-yamux/LICENSE-APACHE) / )
- MIT ([LICENSE-MIT](https://github.com/ChainSafe/js-libp2p-yamux/LICENSE-MIT) / )
# 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.