Skip to content

Commit 6579419

Browse files
authored
Update README.md
1 parent eca6dd3 commit 6579419

File tree

1 file changed

+65
-20
lines changed

1 file changed

+65
-20
lines changed

README.md

Lines changed: 65 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,44 @@
1-
# corestore-swarm-networking
2-
[![Build Status](https://travis-ci.com/andrewosh/corestore-swarm-networking.svg?branch=master)](https://travis-ci.com/andrewosh/corestore-swarm-networking)
1+
# @corestore/networker
2+
[![Build Status](https://travis-ci.com/andrewosh/corestore-networker.svg?branch=master)](https://travis-ci.com/andrewosh/corestore-networker)
33

4-
A corestore networking module that uses [hyperswarm](https://github.com/hyperswarm/network) to discovery peers. This module powers the networking portion of the [Hyperdrive daemon](https://github.com/andrewosh/hyperdrive-daemon).
4+
A corestore networking module that uses [hyperswarm](https://github.com/hyperswarm/network) to discovery peers. This module powers the networking portion of the [Hyperspace](https://github.com/hyperspace-org/hyperspace).
55

6-
Calls to `seed` or `unseed` will not be persisted across restarts, so you'll need to use a separate database that maps discovery keys to network configurations. The Hyperdrive daemon uses [Level](https://github.com/level/level) for this.
6+
Calls to `configure` will not be persisted across restarts, so you'll need to use a separate database that maps discovery keys to network configurations. The Hyperdrive daemon uses [Level](https://github.com/level/level) for this.
77

88
Since corestore has an all-to-all replication model (any shared cores between two peers will be automatically replicated), only one connection needs to be maintained per peer. If multiple connections are opened to a single peer as a result of that peer announcing many keys, then these connections will be automatically deduplicated by comparing NOISE keypairs.
99

10+
### Upgrading from corestore-swarm-networking
11+
This module's going through a major change + a rename as part of our push to develop [Hyperspace](https://github.com/hyperspace-org/hyperspace). If you've previously been using `corestore-swarm-networking` and you'd like to upgrade, [`UPGRADE.md`](/UPGRADE.MD) explains the changes.
12+
1013
### Installation
1114
```
12-
npm i corestore-swarm-networking -g
15+
npm i @corestore/networker
1316
```
1417

1518
### Usage
1619
```js
17-
const SwarmNetworker = require('corestore-swarm-networking')
20+
const Networker = require('@corestore/networker')
1821
const Corestore = require('corestore')
1922
const ram = require('random-access-memory')
2023

2124
const store = new Corestore(ram)
2225
await store.ready()
2326

24-
const networker = new SwarmNetworker(store)
27+
const networker = new Networker(store)
2528

2629
// Start announcing or lookup up a discovery key on the DHT.
27-
await networker.join(discoveryKey, { announce: true, lookup: true })
30+
await networker.configure(discoveryKey, { announce: true, lookup: true })
2831

2932
// Stop announcing or looking up a discovery key.
30-
networker.leave(discoveryKey)
33+
networker.configure(discoveryKey, { announce: false, lookup: false })
3134

3235
// Shut down the swarm (and unnanounce all keys)
3336
await networker.close()
3437
```
3538

3639
### API
3740

38-
#### `const networker = new SwarmNetworker(corestore, networkingOptions = {})`
41+
#### `const networker = new Networker(corestore, networkingOptions = {})`
3942
Creates a new SwarmNetworker that will open replication streams on the `corestore` instance argument.
4043

4144
`networkOpts` is an options map that can include all [hyperswarm](https://github.com/hyperswarm/hyperswarm) options (which will be passed to the internal swarm instance) as well as:
@@ -46,34 +49,76 @@ Creates a new SwarmNetworker that will open replication streams on the `corestor
4649
}
4750
```
4851

49-
#### `await networker.join(discoveryKey, opts = {})`
50-
Join the swarm with the `discoveryKey` argument as the topic.
52+
#### `networker.peers`
53+
The list of currently-connected peers. Each Peer object has the form:
54+
```
55+
{
56+
remotePublicKey: 0xabc..., // The remote peer's NOISE key.
57+
remoteAddress: '10.23.4...:8080', // The remote peer's host/port.
58+
type: 'tcp' | 'utp', // The connection type
59+
stream // The connection's HypercoreProtocol stream
60+
}
61+
```
5162

52-
If this is the first time a `join` or `leave` has been called, the swarm instance will be created automatically.
63+
#### `networker.on('peer-add', peer)`
64+
Emitted when a new connection has been established with `peer`.
5365

54-
Waits for the topic to be fully joined before resolving.
66+
#### `networker.on('peer-remove', peer)`
67+
Emitted when `peer`'s connection has been closed.
68+
69+
#### `await networker.configure(discoveryKey, opts = {})`
70+
Join or leave the swarm with the `discoveryKey` argument as the topic.
71+
72+
If this is the first time `configure` has been called, the swarm instance will be created automatically.
73+
74+
Waits for the topic to be fully joined/left before resolving.
5575

5676
`opts` is an options map of network configuration options that can include:
5777
```js
5878
announce: true, // Announce the discovery key on the swarm
59-
lookup: true // Look up the discovery key on the swarm
79+
lookup: true // Look up the discovery key on the swarm,
80+
flush: true // Wait for a complete swarm flush before resolving.
6081
```
6182

62-
### `networker.joined(discoveryKey)`
83+
#### `networker.joined(discoveryKey)`
6384
Returns `true` if that discovery key is being swarmed.
6485

65-
### `networker.flushed(discoveryKey)`
86+
#### `networker.flushed(discoveryKey)`
6687
Returns true if the swarm has discovered and attempted to connect to all peers announcing `discoveryKey`.
6788

68-
#### `await networker.leave(discoveryKey)`
69-
Stop announcing or looking up the discovery key topic.
89+
#### `networker.listen()`
90+
Starts listening for connections on Hyperswarm's default port.
7091

71-
Waits for the key to be fully unannounced before resolving.
92+
This is called automatically before the first call to `configure`.
7293

7394
#### `await networker.close()`
7495
Shut down the swarm networker.
7596

7697
This will close all replication streams and then destroy the swarm instance. It will wait for all topics to be unannounced, so it might take some time.
7798

99+
### Swarm Extensions
100+
`@corestore/networker` introduces stream-level extensions that operate on each connection. They adhere to Hypercore's [extension API](https://github.com/hypercore-protocol/hypercore#ext--feedregisterextensionname-handlers).
101+
102+
#### `const ext = await networker.registerExtension(name, { encoding, onmessage, onerror })`
103+
Registers an extension with name `name`.
104+
105+
The `onmessage` and `onerror` handlers are both optional methods. `onerror` is an errback, and `onmessage` must have the signature:
106+
107+
```js
108+
function onmessage (msg, peer) {
109+
// `msg` is the (optionally-decoded) message that was received from `peer`.
110+
// `peer` is a `Peer` object (described above in `networker.peers`)
111+
}
112+
```
113+
114+
#### `ext.send(msg, peer)`
115+
Send `msg` (which will optionally be encoded by the extension's encoding opt) to `peer`. `peer` must be a Peer object taken from `networker.peers` or emitted by the networker's `peer-add` event.
116+
117+
#### `ext.broadcast(msg)`
118+
Broadcast `msg` to all currently-connected peers.
119+
120+
#### `ext.destroy()`
121+
Destroy the extension and unregister it from all connections.
122+
78123
### License
79124
MIT

0 commit comments

Comments
 (0)