Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jankaszel/hyperswarm-ws
Joining Hyperswarms via WebSocket gateways
https://github.com/jankaszel/hyperswarm-ws
Last synced: 17 days ago
JSON representation
Joining Hyperswarms via WebSocket gateways
- Host: GitHub
- URL: https://github.com/jankaszel/hyperswarm-ws
- Owner: jankaszel
- License: mit
- Created: 2019-10-29T17:03:13.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2023-05-07T03:43:33.000Z (over 1 year ago)
- Last Synced: 2024-10-17T01:02:06.036Z (22 days ago)
- Language: JavaScript
- Homepage:
- Size: 219 KB
- Stars: 9
- Watchers: 1
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Hyperswarm WebSocket Bridge
Use a WebSocket gateway to bridge Hyperswarm connections via [`hyperswarm-proxy`](https://github.com/RangerMauve/hyperswarm-proxy) to the browser.
```
npm install hyperswarm-ws
```## Usage
Join a Hyperswarm via a WebSocket gateway. For more, look up the [Hyperswarm documentation](https://github.com/hyperswarm/hyperswarm).
```js
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`](https://github.com/maxogden/websocket-stream)).
The class extends `HyperswarmProxyClient` of [`hyperswarm-proxy`](https://github.com/RangerMauve/hyperswarm-proxy), so it will
- use the options specified by `opts`, with
- `opts.pingInterval` being 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 ]`. Will run a WebSocket gateway on the given port that bridges all incoming WebSocket streams into the Hyperswarm network.