|
2 | 2 |
|
3 | 3 | * [pubsub.subscribe](#pubsubsubscribe)
|
4 | 4 | * [pubsub.unsubscribe](#pubsubunsubscribe)
|
| 5 | +* [pubsub.unsubscribeAll](#pubsubunsubscribeall) |
5 | 6 | * [pubsub.publish](#pubsubpublish)
|
6 | 7 | * [pubsub.ls](#pubsubls)
|
7 | 8 | * [pubsub.peers](#pubsubpeers)
|
@@ -82,6 +83,46 @@ ipfs.pubsub.subscribe(topic, receiveMsg, (err) => {
|
82 | 83 | })
|
83 | 84 | ```
|
84 | 85 |
|
| 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 | + |
85 | 126 | A great source of [examples][] can be found in the tests for this API.
|
86 | 127 |
|
87 | 128 | #### `pubsub.publish`
|
|
0 commit comments