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