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
> 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 is the base protocol for libp2p pubsub implementations. This module is responsible for registering the protocol with libp2p, as well as managing the logic regarding pubsub connections with other peers.
16
16
17
17
## Lead Maintainer
18
18
@@ -22,6 +22,7 @@ js-libp2p-pubsub
22
22
23
23
-[Install](#install)
24
24
-[Usage](#usage)
25
+
-[API](#api)
25
26
-[Contribute](#contribute)
26
27
-[License](#license)
27
28
@@ -33,23 +34,34 @@ js-libp2p-pubsub
33
34
34
35
## Usage
35
36
36
-
A pubsubimplementation **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 algorithm, instead of also needing to create the setup for it.
37
38
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 customize their logic. Implementations overriding `start` and `stop`**MUST** call `super`. The `start` function is responsible for registering the pubsub protocol with libp2p, while the `stop` function is responsible for unregistering the pubsub protocol and closing pubsub connections.
39
42
40
43
All the remaining functions **MUST NOT** be overwritten.
41
44
42
45
The following example aims to show how to create your pubsub implementation extending this base protocol. The pubsub implementation will handle the subscriptions logic.
@@ -65,21 +77,131 @@ class PubsubImplementation extends Pubsub {
65
77
unsubscribe() {
66
78
// Required to be implemented by the subclass
67
79
}
80
+
81
+
getTopics() {
82
+
// Required to be implemented by the subclass
83
+
}
68
84
}
69
85
```
70
86
87
+
## API
88
+
89
+
The following specified API should be the base API for a pubsub implementation on top of `libp2p`.
90
+
91
+
### Start
92
+
93
+
Starts the pubsub subsystem. The protocol will be registered to `libp2p`, which will result in pubsub being notified when peers who support the protocol connect/disconnect to `libp2p`.
94
+
95
+
#### `pubsub.start()`
96
+
97
+
##### Returns
98
+
99
+
| Type | Description |
100
+
|------|-------------|
101
+
|`Promise<void>`| resolves once pubsub starts |
102
+
103
+
### Stop
104
+
105
+
Stops the pubsub subsystem. The protocol will be unregistered from `libp2p`, which will remove all listeners for the protocol and the established connections will be closed.
106
+
107
+
#### `pubsub.stop()`
108
+
109
+
##### Returns
110
+
111
+
| Type | Description |
112
+
|------|-------------|
113
+
|`Promise<void>`| resolves once pubsub stops |
114
+
115
+
### Publish
116
+
117
+
Publish data messages to pubsub topics.
118
+
119
+
#### `pubsub.publish(topics, messages)`
120
+
121
+
##### Parameters
122
+
123
+
| Name | Type | Description |
124
+
|------|------|-------------|
125
+
| topics | `Array<string>|string` | set of pubsub topics |
126
+
| messages | `Array<any>|any` | set of messages to publish |
127
+
128
+
##### Returns
129
+
130
+
| Type | Description |
131
+
|------|-------------|
132
+
|`Promise<void>`| resolves once messages are published to the network |
133
+
134
+
### Subscribe
135
+
136
+
Subscribe to the given topic(s).
137
+
138
+
#### `pubsub.subscribe(topics)`
139
+
140
+
##### Parameters
141
+
142
+
| Name | Type | Description |
143
+
|------|------|-------------|
144
+
| topics | `Array<string>|string` | set of pubsub topics |
145
+
146
+
### Unsubscribe
147
+
148
+
Unsubscribe from the given topic(s).
149
+
150
+
#### `pubsub.unsubscribe(topics)`
151
+
152
+
##### Parameters
153
+
154
+
| Name | Type | Description |
155
+
|------|------|-------------|
156
+
| topics | `Array<string>|string` | set of pubsub topics |
157
+
158
+
### Get Topics
159
+
160
+
Get the list of topics which the peer is subscribed to.
161
+
162
+
#### `pubsub.getTopics()`
163
+
164
+
##### Returns
165
+
166
+
| Type | Description |
167
+
|------|-------------|
168
+
|`Array<String>`| Array of subscribed topics |
169
+
170
+
### Get Peers Subscribed to a topic
171
+
172
+
Get a list of the [PeerId](https://github.com/libp2p/js-peer-id) strings that are subscribed to one topic.
173
+
174
+
#### `pubsub.getPeersSubscribed(topic)`
175
+
176
+
##### Parameters
177
+
178
+
| Name | Type | Description |
179
+
|------|------|-------------|
180
+
| topic |`string`| pubsub topic |
181
+
182
+
##### Returns
183
+
184
+
| Type | Description |
185
+
|------|-------------|
186
+
|`Array<string>`| Array of base-58 PeerId's |
187
+
71
188
### Validate
72
189
73
190
Validates the signature of a message.
74
191
75
-
#### `pubsub.validate(message, callback)`
192
+
#### `pubsub.validate(message)`
76
193
77
194
##### Parameters
78
195
79
196
| Name | Type | Description |
80
197
|------|------|-------------|
81
198
| message |`Message`| a pubsub message |
82
-
| callback |`function(Error, Boolean)`| calls back with true if the message is valid |
199
+
200
+
#### Returns
201
+
202
+
| Type | Description |
203
+
|------|-------------|
204
+
|`Promise<Boolean>`| resolves to true if the message is valid |
83
205
84
206
## Implementations using this base protocol
85
207
@@ -94,8 +216,6 @@ Feel free to join in. All welcome. Open an [issue](https://github.com/libp2p/js-
94
216
95
217
This repository falls under the IPFS [Code of Conduct](https://github.com/ipfs/community/blob/master/code-of-conduct.md).
0 commit comments