hyperswarm-ws v1.2.0
Hyperswarm WebSocket Bridge
Use a WebSocket gateway to bridge Hyperswarm connections via hyperswarm-proxy to the browser.
npm install hyperswarm-wsUsage
Join a Hyperswarm via a WebSocket gateway. For more, look up the Hyperswarm documentation.
const crypto = require('crypto')
const { ClientSwarm } = require('hyperswarm-ws')
// look for peers listed under this topic
const topic = crypto
.createHash('sha256')
.update('my-hyperswarm-topic')
.digest()
const swarm = new ClientSwarm('ws://localhost:4200')
swarm.join(topic)API
swarm = new ClientSwarm(gateway, [opts])
Create a new client instance that connects to a WebSocket gateway given the a gateway URL. gateway may alternatively be a websocket connection stream (created using websocket-stream).
The class extends HyperswarmProxyClient of hyperswarm-proxy, so it will
- use the options specified by
opts, withopts.pingIntervalbeing the interval used for pinging clients (default: 3500 ms),
- pass all other opts to its parent constructor, and
- inherit all instance methods, such as
reconnect,join,leave,destroy.
swarm = async createClientSwarm(gatewayUrls)
Given an array of gateway URLs, try all gateways for working connections until the first one is found. Will pass that URL as websocket stream to a new ClientSwarm instance.
If none of the gateways works, an error with err.code = 'EBADGATEWAYS' is thrown.
Running a Gateway
For running a gateway, use hyperswarm-ws-gateway [-p <port>]. Will run a WebSocket gateway on the given port that bridges all incoming WebSocket streams into the Hyperswarm network.