# dht-universal

> Universal wrapper for @hyperswarm/dht and @hyperswarm/dht-relay working in Node.js and the Browser

Latest version **0.4.1** (published 2022-03-30) · MIT license · 0 weekly downloads

## Install

```sh
npm install dht-universal
pnpm add dht-universal
yarn add dht-universal
bun add dht-universal
```

## Health

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

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.4.1 |
| Published | 2022-03-30 |
| First published | 2022-01-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 3 |
| Unpacked size | 30.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Maintainers | nzh |
| Keywords | dht, hyperswarm, dht-relay, dht-universal, universal, nodejs, browser |

## Links

- npm: https://www.npmjs.com/package/dht-universal
- Repository: https://github.com/synonymdev/dht-universal
- Homepage: https://github.com/synonymdev/dht-universal#readme
- Issues: https://github.com/synonymdev/dht-universal/issues
- npm.io page: https://npm.io/package/dht-universal

## Dependencies (3)

- [isomorphic-ws](https://npm.io/package/isomorphic-ws.md) ^4.0.1
- [@hyperswarm/dht](https://npm.io/package/@hyperswarm/dht.md) ^5.0.17
- [@hyperswarm/dht-relay](https://npm.io/package/@hyperswarm/dht-relay.md) ^0.2.2

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

- 0.4.1 (latest) — 2022-03-30
- 0.4.0 — 2022-03-20
- 0.3.2 — 2022-02-18
- 0.3.1 — 2022-02-10
- 0.3.0 — 2022-02-10
- 0.2.1 — 2022-02-07
- 0.2.0 — 2022-02-07
- 0.1.1 — 2022-02-07
- 0.1.0 — 2022-02-06
- 0.0.8 — 2022-01-15
- 0.0.7 — 2022-01-15
- 0.0.6 — 2022-01-11
- 0.0.5 — 2022-01-11
- 0.0.4 — 2022-01-10
- 0.0.3 — 2022-01-10
- … 2 more at https://npm.io/package/dht-universal/versions

## README

# dht-universal

Universal wrapper for `@hyperswarm/dht` and `@hyperswarm/dht-relay` working in Node.js and the Browser

## Installation

```sh
npm install dht-universal
```

## Usage

In browser, it will use the `@hyperswarm/dht-relay` with a `WebSocket` transport, so you need to pass a relay URL to the constructor, and wait for it to be ready to confirm the websocket is open.

### Drop-in replacement initialization

```js
import { DHT } from 'dht-universal';
const node = new DHT({ relay: 'wss://dht-relay.example.com/' });
await node.ready();
```

### Async initialization

You can also use the static async function `create` to try multiple relays until one works.

```js
import { DHT } from 'dht-universal';

const node = await DHT.create({
  relays: ['wss://dht-relay.example.com/'],
  ...opts,
});
// No need to call `await node.ready()`
```

### Relay in Node environment

If you want to use the relay in Node, you can import it explicitly:

```js
import { DHT } from 'dht-universal/relay.js';

const node = await DHT.create({
  relays: ['wss://dht-relay.example.com/'],
  ...opts,
});
```

## API

Should be the same as [@hyperswarm/dht](https://github.com/hyperswarm/dht#api).

### API test coverage:

The goal of this module is to ensure the usability of the DHT in the browser within the common use cases namely Hyperswarm and replicating Hypercores through Corestore:

```js
import Corestore from 'corestore';
import Hyperswarm from 'hyperswarm';
import ram from 'random-access-memory';
import DHT from 'dht-universal';

(async () => {
  const dht = new DHT({ relay: 'wss://dht-relay.example.com/' });
  const swarm = new Hyperswarm({ dht });
  const store = new Corestore(ram);
  await store.ready();

  swarm.on('connection', (socket) => {
    store.replicate(socket);
  });

  const core = store.get({ key: <0x012...def>});
  await core.ready();

  swarm.join(core.discoveryKey, { server: true, client: true });
  await swarm.flush();

  await core.update();
  const block = await core.get(core.length - 1);
})();
```

Other unit tests are available but might be removed later.

## Setup Relay

This module exposes a helper function to easily run a relay server.

```js
import { setupRelay } from 'dht-universal/setup-relay.js';

const { port, closeRelay } = await setupRelay({ dhtOpts, wsServerOptions });
```

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