Skip to content
This repository was archived by the owner on Jun 27, 2023. It is now read-only.

Commit d31659e

Browse files
committed
feat: use registrar
1 parent 9df6594 commit d31659e

File tree

14 files changed

+585
-818
lines changed

14 files changed

+585
-818
lines changed

.aegir.js

Lines changed: 0 additions & 63 deletions
This file was deleted.

README.md

Lines changed: 116 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ js-libp2p-pubsub
1212
[![standard-readme compliant](https://img.shields.io/badge/standard--readme-OK-green.svg?style=flat-square)](https://github.com/RichardLitt/standard-readme)
1313
[![](https://img.shields.io/badge/pm-waffle-yellow.svg?style=flat-square)](https://waffle.io/libp2p/js-libp2p-pubsub)
1414

15-
> libp2p-pubsub consits on the base protocol for libp2p pubsub implementation. This module is responsible for all the logic regarding peer connections.
15+
> libp2p-pubsub consists on the base protocol for libp2p pubsub implementations. This module is responsible for registering the protocol in libp2p, as well as all the logic regarding pubsub connections with other peers.
1616
1717
## Lead Maintainer
1818

@@ -22,6 +22,7 @@ js-libp2p-pubsub
2222

2323
- [Install](#install)
2424
- [Usage](#usage)
25+
- [API](#api)
2526
- [Contribute](#contribute)
2627
- [License](#license)
2728

@@ -33,23 +34,27 @@ js-libp2p-pubsub
3334

3435
## Usage
3536

36-
A pubsub implementation **MUST** override the `_processConnection`, `publish`, `subscribe` and `unsubscribe` functions.
37+
`libp2p-pubsub` abstracts the implementation protocol registration within `libp2p` and takes care of all the protocol connections. This way, a pubsub implementation can focus on its routing algortithm, instead of also needing to create the setup for it.
3738

38-
Other functions, such as `_addPeer`, `_removePeer`, `_onDial`, `start` and `stop` may be overwritten if the pubsub implementation needs to add custom logic on them. It is important pointing out that `start` and `stop` **must** call `super`. The `start` function is responsible for mounting the pubsub protocol onto the libp2p node and sending its' subscriptions to every peer connected, while the `stop` function is responsible for unmounting the pubsub protocol and shutting down every connection
39+
A pubsub implementation **MUST** override the `_processMessages`, `publish`, `subscribe`, `unsubscribe` and `getTopics` functions.
40+
41+
Other functions, such as `_onPeerConnected`, `_onPeerDisconnected`, `_addPeer`, `_removePeer`, `start` and `stop` may be overwritten if the pubsub implementation needs to add custom logic on them. It is important pointing out that `start` and `stop` **must** call `super`. The `start` function is responsible for registering the pubsub protocol onto the libp2p node, while the `stop` function is responsible for unregistering the pubsub protocol and shutting down every connection
3942

4043
All the remaining functions **MUST NOT** be overwritten.
4144

4245
The following example aims to show how to create your pubsub implementation extending this base protocol. The pubsub implementation will handle the subscriptions logic.
4346

47+
TODO: add explanation for registrar!
48+
4449
```JavaScript
4550
const Pubsub = require('libp2p-pubsub')
4651

4752
class PubsubImplementation extends Pubsub {
48-
constructor(libp2p) {
49-
super('libp2p:pubsub', '/pubsub-implementation/1.0.0', libp2p)
53+
constructor(peerInfo, registrar, options = {}) {
54+
super('libp2p:pubsub', '/pubsub-implementation/1.0.0', peerInfo, registrar, options)
5055
}
5156

52-
_processConnection(idB58Str, conn, peer) {
57+
_processMessages(idB58Str, conn, peer) {
5358
// Required to be implemented by the subclass
5459
// Process each message accordingly
5560
}
@@ -65,9 +70,114 @@ class PubsubImplementation extends Pubsub {
6570
unsubscribe() {
6671
// Required to be implemented by the subclass
6772
}
73+
74+
getTopics() {
75+
// Required to be implemented by the subclass
76+
}
6877
}
6978
```
7079

80+
## API
81+
82+
The following specified API should be the base API for a pubsub implementation on top of `libp2p`.
83+
84+
### Start
85+
86+
Start the pubsub subsystem. The protocol will be registered to `libp2p`, which will notify about peers being connected and disconnected with the protocol.
87+
88+
#### `pubsub.start()`
89+
90+
##### Returns
91+
92+
| Type | Description |
93+
|------|-------------|
94+
| `Promise<void>` | resolves once pubsub starts |
95+
96+
### Stop
97+
98+
Stop the pubsub subsystem. The protocol will be unregistered to `libp2p`, which will remove all listeners for the protocol and the established connections will be closed.
99+
100+
#### `pubsub.stop()`
101+
102+
##### Returns
103+
104+
| Type | Description |
105+
|------|-------------|
106+
| `Promise<void>` | resolves once pubsub stops |
107+
108+
### Publish
109+
110+
Publish data messages to pubsub topics.
111+
112+
#### `pubsub.publish(topics, messages)`
113+
114+
##### Parameters
115+
116+
| Name | Type | Description |
117+
|------|------|-------------|
118+
| topics | `Array<string>|string` | set of pubsub topics |
119+
| messages | `Array<any>|any` | set of messages to publish |
120+
121+
##### Returns
122+
123+
| Type | Description |
124+
|------|-------------|
125+
| `Promise<void>` | resolves once messages are published to the network |
126+
127+
### Subscribe
128+
129+
Subscribe to the given topic(s).
130+
131+
#### `pubsub.subscribe(topics)`
132+
133+
##### Parameters
134+
135+
| Name | Type | Description |
136+
|------|------|-------------|
137+
| topics | `Array<string>|string` | set of pubsub topics |
138+
139+
### Unsubscribe
140+
141+
Unsubscribe from the given topic(s).
142+
143+
#### `pubsub.unsubscribe(topics)`
144+
145+
##### Parameters
146+
147+
| Name | Type | Description |
148+
|------|------|-------------|
149+
| topics | `Array<string>|string` | set of pubsub topics |
150+
151+
### Get Topics
152+
153+
Get the list of topics which the peer is subscribed to.
154+
155+
#### `pubsub.getTopics()`
156+
157+
##### Returns
158+
159+
| Type | Description |
160+
|------|-------------|
161+
| `Array<String>` | Array of subscribed topics |
162+
163+
### Get Peers Subscribed to a topic
164+
165+
Get a list of the peer-ids that are subscribed to one topic.
166+
167+
#### `pubsub.getPeersSubscribed(topic)`
168+
169+
##### Parameters
170+
171+
| Name | Type | Description |
172+
|------|------|-------------|
173+
| topic | `string` | pubsub topic |
174+
175+
##### Returns
176+
177+
| Type | Description |
178+
|------|-------------|
179+
| `Array<string>` | Array of base-58 peer id's |
180+
71181
### Validate
72182

73183
Validates the signature of a message.

package.json

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717
"coverage": "aegir coverage",
1818
"coverage-publish": "aegir coverage --provider coveralls"
1919
},
20-
"browser": {
21-
"test/utils/nodejs-bundle": "./test/utils/browser-bundle.js"
22-
},
2320
"files": [
2421
"src",
2522
"dist"
@@ -45,35 +42,26 @@
4542
},
4643
"homepage": "https://github.com/libp2p/js-libp2p-pubsub#readme",
4744
"devDependencies": {
48-
"aegir": "^20.3.1",
45+
"aegir": "^20.4.1",
4946
"benchmark": "^2.1.4",
5047
"chai": "^4.2.0",
5148
"chai-spies": "^1.0.0",
5249
"dirty-chai": "^2.0.1",
53-
"libp2p": "~0.26.2",
54-
"libp2p-secio": "~0.11.1",
55-
"libp2p-spdy": "~0.13.3",
56-
"libp2p-tcp": "~0.13.1",
57-
"libp2p-websocket-star": "~0.10.2",
58-
"libp2p-websocket-star-rendezvous": "~0.4.1",
59-
"lodash": "^4.17.15",
50+
"it-pair": "^1.0.0",
6051
"multiaddr": "^6.1.0",
61-
"peer-id": "~0.12.2",
62-
"peer-info": "~0.15.1"
52+
"peer-id": "~0.13.3",
53+
"peer-info": "~0.17.0"
6354
},
6455
"dependencies": {
65-
"async": "^2.6.2",
6656
"bs58": "^4.0.1",
6757
"debug": "^4.1.1",
6858
"err-code": "^2.0.0",
69-
"length-prefixed-stream": "^2.0.0",
59+
"it-length-prefixed": "^2.0.0",
60+
"it-pipe": "^1.0.1",
61+
"it-pushable": "^1.3.2",
7062
"libp2p-crypto": "~0.17.0",
7163
"protons": "^1.0.1",
72-
"pull-length-prefixed": "^1.3.3",
73-
"pull-pushable": "^2.2.0",
74-
"pull-stream": "^3.6.14",
75-
"sinon": "^7.5.0",
76-
"time-cache": "~0.3.0"
64+
"sinon": "^7.5.0"
7765
},
7866
"contributors": [
7967
"Cayman <[email protected]>",

0 commit comments

Comments
 (0)