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
{{ message }}
This repository was archived by the owner on Jun 27, 2023. It is now read-only.
> 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.
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,27 @@ 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 algortithm, 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 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
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,9 +70,114 @@ class PubsubImplementation extends Pubsub {
65
70
unsubscribe() {
66
71
// Required to be implemented by the subclass
67
72
}
73
+
74
+
getTopics() {
75
+
// Required to be implemented by the subclass
76
+
}
68
77
}
69
78
```
70
79
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.
0 commit comments