# hyperswarm-web

> Implementation of the hyperswarm API for use in web browsers

Latest version **2.2.0** (published 2022-02-15) · MIT license · 0 weekly downloads

## Install

```sh
npm install hyperswarm-web
pnpm add hyperswarm-web
yarn add hyperswarm-web
bun add hyperswarm-web
```

Provides the command `hyperswarm-web`.

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.2.0 |
| Published | 2022-02-15 |
| First published | 2020-01-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 6 |
| Unpacked size | 14.7 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 106 |
| Author | rangermauve |
| Maintainers | rangermauve |
| Keywords | hyperswarm, proxy, web, browser, webrtc |

## Links

- npm: https://www.npmjs.com/package/hyperswarm-web
- Repository: https://github.com/RangerMauve/hyperswarm-web
- Homepage: https://github.com/RangerMauve/hyperswarm-web#readme
- Issues: https://github.com/RangerMauve/hyperswarm-web/issues
- npm.io page: https://npm.io/package/hyperswarm-web

## Dependencies (6)

- [send](https://npm.io/package/send.md) ^0.17.1
- [minimist](https://npm.io/package/minimist.md) ^1.2.0
- [duplexpair](https://npm.io/package/duplexpair.md) ^1.0.1
- [websocket-stream](https://npm.io/package/websocket-stream.md) ^5.5.2
- [hyperswarm-proxy-ws](https://npm.io/package/hyperswarm-proxy-ws.md) ^1.2.0
- [@geut/discovery-swarm-webrtc](https://npm.io/package/@geut/discovery-swarm-webrtc.md) ^4.0.1

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 2.2.0 (latest) — 2022-02-15
- 2.1.1 — 2020-09-26
- 2.1.0 — 2020-09-09
- 2.0.2 — 2020-09-03
- 2.0.0 — 2020-08-24
- 1.0.3 — 2020-04-11
- 1.0.2 — 2020-04-10
- 1.0.1 — 2020-01-06
- 1.0.0 — 2020-01-02

## README

# hyperswarm-web
Implementation of the hyperswarm API for use in web browsers


## Using in an application

```
npm i -s hyperswarm-web
```

```js
// Based on example in hyperswarm repo
// Try running the regular hyperswarm demo with node
const hyperswarm = require('hyperswarm-web')
const crypto = require('crypto')

const swarm = hyperswarm({
  // Specify a server list of HyperswarmServer instances
  bootstrap: ['ws://yourhyperswarmserver.com'],
  // You can also specify proxy and signal servers separated
  wsProxy: [
    'ws://proxy1.com',
    'ws://proxy2.com'
  ],
  webrtcBootstrap: [
    'ws://signal1.com',
    'ws://signal2.com'
  ],
  // The configuration passed to the SimplePeer constructor
  // See https://github.com/feross/simple-peer#peer--new-peeropts
  // for more options
  simplePeer: {
    // The configuration passed to the RTCPeerConnection constructor, for more details see
    // https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/RTCPeerConnection#RTCConfiguration_dictionary
    config: {
      // List of STUN and TURN servers to connect
      // Without the connection is limited to local peers
      iceServers: require('./ice-servers.json')
    }
  },
  // Maximum number of peers (optional)
  // Used in both webrtc (default 5) and ws proxy config (default 24)
  maxPeers: 10,
  // Websocket reconnect delay in milliseconds (optional) (default 1000)
  wsReconnectDelay: 5000
})

// look for peers listed under this topic
const topic = crypto.createHash('sha256')
  .update('my-hyperswarm-topic')
  .digest()

swarm.join(topic)

swarm.on('connection', (socket, details) => {
  console.log('new connection!', details)

  // you can now use the socket as a stream, eg:
  // socket.pipe(hypercore.replicate()).pipe(socket)
})

swarm.on('disconnection', (socket, details) => {
  console.log(details.peer.host, 'disconnected!')
  console.log('now we have', swarm.peers.length, 'peers!')
})
```

Build it with [Browserify](http://browserify.org/) to get it running on the web.

You could also compile an existing codebase relying on hyperswarm to run on the web by adding a `browser` field set to `{"hyperswarm": "hyperswarm-web"}` to have Browserify alias it when compiling dependencies.

## Setting up a proxy server

`HyperswarmServer` provides two services:

  - [HyperswarmProxyWS](https://github.com/RangerMauve/hyperswarm-proxy-ws): to proxy hyperswarm connections over websockets. Path: `ws://yourserver/proxy`
  - [SignalServer](https://github.com/geut/discovery-swarm-webrtc#server): for P2P WebRTC signaling connections. Path: `ws://yourserver/signal`

Running a `HyperswarmServer` will allows you to use both services in one single process.

```
npm i -g hyperswarm-web

# Run it! Default port is 4977 (HYPR on a phone pad)
hyperswarm-web

# Run it with a custom port
hyperswarm-web --port 42069
```

### Running as a Linux service with SystemD

```bash
sudo cat << EOF > /etc/systemd/system/hyperswarm-web.service
[Unit]
Description=Hyperswarm proxy server which webpages can connect to.

[Service]
Type=simple
# Check that hyperswarm-web is present at this location
# If it's not, replace the path with its location
# You can get the location with 'whereis hyperswarm-web'
# Optionally add a --port parameter if you don't want 4977
ExecStart=/usr/local/bin/hyperswarm-web
Restart=always

[Install]
WantedBy=multi-user.target
EOF

sudo chmod 644 /etc/systemd/system/hyperswarm-web.service

sudo systemctl daemon-reload
sudo systemctl enable hyperswarm-web
sudo systemctl start hyperswarm-web

sudo systemctl status hyperswarm-web
```

---
_Source: https://npm.io/package/hyperswarm-web · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
