Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit 78d3816

Browse files
committed
DOC: prepear documentation interface change
1 parent 45da688 commit 78d3816

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

SPEC/PUBSUB.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
* [pubsub.subscribe](#pubsubsubscribe)
44
* [pubsub.unsubscribe](#pubsubunsubscribe)
5+
* [pubsub.unsubscribeAll](#pubsubunsubscribeall)
56
* [pubsub.publish](#pubsubpublish)
67
* [pubsub.ls](#pubsubls)
78
* [pubsub.peers](#pubsubpeers)
@@ -82,6 +83,46 @@ ipfs.pubsub.subscribe(topic, receiveMsg, (err) => {
8283
})
8384
```
8485

86+
#### `pubsub.unsubscribeAll`
87+
88+
> Unsubscribes from a pubsub topic.
89+
90+
##### Go **WIP**
91+
92+
##### JavaScript - `ipfs.pubsub.unsubscribe(topic, handler, [callback])`
93+
94+
- `topic: String` - The topic to unsubscribe from
95+
- `callback: (Error) => {}` (Optional) Called once the unsubscribe is done.
96+
97+
If no `callback` is passed, a [promise][] is returned.
98+
99+
This works like `EventEmitter.removeAllListeners`, The underlying subscription will only be canceled once all listeners for a topic have been removed.
100+
101+
**Example:**
102+
103+
```JavaScript
104+
const topic = 'fruit-of-the-day'
105+
106+
ipfs.pubsub.subscribe(topic, (msg) => console.log(msg.toString()), (err) => {
107+
if (err) {
108+
return console.error(`failed to subscribe to ${topic}`, err)
109+
}
110+
111+
console.log(`subscribed to ${topic}`)
112+
113+
// unsubscribe a second later
114+
setTimeout(() => {
115+
ipfs.pubsub.unsubscribeAll(topic,(err) => {
116+
if (err) {
117+
return console.error(`failed to unsubscribe from ${topic}`, err)
118+
}
119+
120+
console.log(`unsubscribed from ${topic}`)
121+
})
122+
}, 1000)
123+
})
124+
```
125+
85126
A great source of [examples][] can be found in the tests for this API.
86127

87128
#### `pubsub.publish`

0 commit comments

Comments
 (0)