cf-libp2p-ws-transport
TypeScript icon, indicating that this package has built-in type declarations

2.0.4 • Public • Published

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 { 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

Package Sidebar

Install

npm i cf-libp2p-ws-transport

Weekly Downloads

5

Version

2.0.4

License

Apache-2.0 OR MIT

Unpacked Size

31 kB

Total Files

12

Last publish

Collaborators

  • alanshaw