1.1.1 • Published 2 years ago

cf-libp2p-ws-transport v1.1.1

Weekly downloads
-
License
Apache-2.0 OR MIT
Repository
github
Last release
2 years ago

cf-libp2p-ws-transport

Build JavaScript Style Guide

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 { Mplex } from '@libp2p/mplex'
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) {
    const wsTransport = new WebSockets()
    const node = await createLibp2p({
      addresses: { listen: [listenAddr] },
      transports: [wsTransport],
      streamMuxers: [new Mplex()],
      connectionEncryption: [new Noise()]
    })

    node.handle(echoProtocol, ({ stream }) => pipe(stream, stream))

    await node.start()

    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