This module is a JavaScript implementation of Yamux from Hashicorp designed to be used with js-libp2p.
Formerly published as @chainsafe/libp2p-yamux.
@chainsafe/libp2p-yamux
import { createLibp2p } from 'libp2p'import { yamux } from '@libp2p/yamux'const node = await createLibp2p({ // ... other options streamMuxers: [ yamux() ]}) Copy
import { createLibp2p } from 'libp2p'import { yamux } from '@libp2p/yamux'const node = await createLibp2p({ // ... other options streamMuxers: [ yamux() ]})
import { yamux } from '@libp2p/yamux'import { multiaddrConnectionPair } from '@libp2p/utils'// a pair of connected multiaddr connections - in a real application these// would be the two ends of a network connectionconst [outboundConnection, inboundConnection] = multiaddrConnectionPair()const clientMuxer = yamux()().createStreamMuxer(outboundConnection)const serverMuxer = yamux()().createStreamMuxer(inboundConnection)// echo incoming data back on the server sideserverMuxer.addEventListener('stream', (evt) => { const stream = evt.detail stream.addEventListener('message', (msg) => { stream.send(msg.data) })})// open a stream from the client and send some dataconst stream = await clientMuxer.createStream()const received = new Promise<string>((resolve) => { stream.addEventListener('message', (msg) => { resolve(new TextDecoder().decode(msg.data.subarray())) })})stream.send(new TextEncoder().encode('hello world'))console.info(await received)// -> hello worldawait stream.close()await clientMuxer.close()await serverMuxer.close() Copy
import { yamux } from '@libp2p/yamux'import { multiaddrConnectionPair } from '@libp2p/utils'// a pair of connected multiaddr connections - in a real application these// would be the two ends of a network connectionconst [outboundConnection, inboundConnection] = multiaddrConnectionPair()const clientMuxer = yamux()().createStreamMuxer(outboundConnection)const serverMuxer = yamux()().createStreamMuxer(inboundConnection)// echo incoming data back on the server sideserverMuxer.addEventListener('stream', (evt) => { const stream = evt.detail stream.addEventListener('message', (msg) => { stream.send(msg.data) })})// open a stream from the client and send some dataconst stream = await clientMuxer.createStream()const received = new Promise<string>((resolve) => { stream.addEventListener('message', (msg) => { resolve(new TextDecoder().decode(msg.data.subarray())) })})stream.send(new TextEncoder().encode('hello world'))console.info(await received)// -> hello worldawait stream.close()await clientMuxer.close()await serverMuxer.close()
This module is a JavaScript implementation of Yamux from Hashicorp designed to be used with js-libp2p.
Formerly published as
@chainsafe/libp2p-yamux.Example: Configure libp2p with Yamux
Example: Using the low-level API