cf-libp2p-ws-transport
v2.0.4
Published
Libp2p WebSocket transport for Cloudflare WebSockets
Downloads
48
Maintainers
Readme
cf-libp2p-ws-transport
Libp2p WebSocket transport for Cloudflare WebSockets.
Install
npm install cf-libp2p-ws-transport
Usage
import { createLibp2p } from 'libp2p'
import { WebSockets } from 'cf-libp2p-ws-transport'
import { noise } from '@chainsafe/libp2p-noise'
import { yamux } from '@chainsafe/libp2p-yamux'
import { pipe } from 'it-pipe'
const listenAddr = '/ip4/127.0.0.1/tcp/1234/ws'
const echoProtocol = '/test/echo/1.0.0'
export default {
async fetch (request) {
let wsTransport
const node = await createLibp2p({
addresses: { listen: [listenAddr] },
transports: [components => {
wsTransport = new WebSockets(components)
return wsTransport
}],
streamMuxers: [yamux()],
connectionEncrypters: [noise()]
})
node.handle(echoProtocol, ({ stream }) => pipe(stream, stream))
const listener = wsTransport.listenerForMultiaddr(listenAddr)
return listener.handleRequest(request)
}
}
Contributing
Feel free to join in. All welcome. Open an issue!
License
Dual-licensed under MIT + Apache 2.0