Yamux stream multiplexer for libp2p
The npm package "@chainsafe/libp2p-yamux" is a vital tool for developers working with the libp2p networking stack, specifically designed to enhance the handling of multiple data streams over a single connection. This stream multiplexer, based on the Yamux protocol, is crucial for improving the efficiency and scalability of network connections in decentralized applications. By using "@chainsafe/libp2p-yamux", developers can initiate, divide, and manage several streams of data concurrently without the need to establish multiple connections, thus optimizing both the speed and resource usage of their applications. This package ensures that data flows are more organized and less prone to congestion, a common challenge in peer-to-peer network communications.
Installation of this package is straightforward with the npm command, which can be executed by running `npm install @chainsafe/libp2p-yamux` in the terminal. This command seamlessly integrates the Yamux stream multiplexer into your project, setting the stage for more robust and efficient data handling capabilities. Once installed, "@chainsafe/libp2p-yamux" allows for the simultaneous transmission of various data types, such as files, messages, and real-time media, all within a single, unified network layer. This functionality is particularly beneficial in environments where high throughput and low latency are critical, making it a preferred choice among developers working on complex networking applications.
The integration of "@chainsafe/libp2p-yamux" into your libp2p project not only boosts performance but also contributes to better resource management and error handling. The multiplexer’s ability to prioritize data streams ensures that more critical data can be transmitted with higher priority, which is essential in maintaining the quality of service in network communications. Additionally, the use of this npm package can significantly reduce the complexity of network programming by abstracting the details of stream management, allowing developers to focus more on the core functionalities of their applications rather than the underlying network logistics. With its robust features and reliable performance, "@chainsafe/libp2p-yamux" is a key component in building efficient and scalable peer-to-p2p networks.
Core dependencies of this npm package and its dev dependencies.
@libp2p/interface, @libp2p/utils, get-iterator, it-foreach, it-pushable, it-stream-types, uint8arraylist, @dapplion/benchmark, @libp2p/interface-compliance-tests, @libp2p/logger, @libp2p/mplex, aegir, it-drain, it-pair, it-pipe
A README file for the @chainsafe/libp2p-yamux code repository. View Code
Yamux stream multiplexer for libp2p
This module is a JavaScript implementation of Yamux from Hashicorp designed to be used with js-libp2p.
import { createLibp2p } from 'libp2p'
import { yamux } from '@chainsafe/libp2p-yamux'
const node = await createLibp2p({
// ... other options
streamMuxers: [
yamux()
]
})
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()
$ npm i @chainsafe/libp2p-yamux
<script>
tagLoading this module through a script tag will make it's exports available as ChainsafeLibp2pYamux
in the global namespace.
<script src="https://unpkg.com/@chainsafe/libp2p-yamux/dist/index.min.js"></script>
Licensed under either of
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.