You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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).
5
5
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.
7
7
8
8
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.
9
9
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.
#### `const networker = new SwarmNetworker(corestore, networkingOptions = {})`
41
+
#### `const networker = new Networker(corestore, networkingOptions = {})`
39
42
Creates a new SwarmNetworker that will open replication streams on the `corestore` instance argument.
40
43
41
44
`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
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.
55
75
56
76
`opts` is an options map of network configuration options that can include:
57
77
```js
58
78
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.
60
81
```
61
82
62
-
### `networker.joined(discoveryKey)`
83
+
####`networker.joined(discoveryKey)`
63
84
Returns `true` if that discovery key is being swarmed.
64
85
65
-
### `networker.flushed(discoveryKey)`
86
+
####`networker.flushed(discoveryKey)`
66
87
Returns true if the swarm has discovered and attempted to connect to all peers announcing `discoveryKey`.
67
88
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.
70
91
71
-
Waits for the key to be fully unannounced before resolving.
92
+
This is called automatically before the first call to `configure`.
72
93
73
94
#### `await networker.close()`
74
95
Shut down the swarm networker.
75
96
76
97
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.
77
98
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).
The `onmessage` and `onerror` handlers are both optional methods. `onerror` is an errback, and `onmessage` must have the signature:
106
+
107
+
```js
108
+
functiononmessage (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.
0 commit comments