// Connect two yamux muxers to demo basic stream multiplexing functionality
constclientMuxer = yamux({ client:true, onIncomingStream:stream=> { // echo data on incoming streams pipe(stream, stream) }, onStreamEnd:stream=> { // do nothing } })()
constserverMuxer = 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 constp = duplexPair()
// connect the muxers together pipe(p[0], clientMuxer, p[0]) pipe(p[1], serverMuxer, p[1])
// now either side can open streams conststream0 = clientMuxer.newStream() conststream1 = serverMuxer.newStream()
// Send some data to the other side constencoder = newTextEncoder() constdata = [encoder.encode('hello'), encoder.encode('world')] pipe(data, stream0)
// Receive data back constresult = awaitpipe(stream0, all)
This module is a JavaScript implementation of Yamux from Hashicorp designed to be used with js-libp2p.
Example: Configure libp2p with Yamux
Example: Using the low-level API