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

Commit 689c9cf

Browse files
update pubsub spec
1 parent f02aaea commit 689c9cf

File tree

1 file changed

+30
-21
lines changed

1 file changed

+30
-21
lines changed

API/pubsub/README.md

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,47 @@ pubsub API
33

44
#### `pubsub.subscribe`
55

6-
> Subscribe to an IPFS Topic
6+
> Subscribe to a pubsub topic.
77
88
##### `Go` **WIP**
99

10-
##### `JavaScript` - ipfs.pubsub.subscribe(topic, options, callback)
10+
##### `JavaScript` - ipfs.pubsub.subscribe(topic, options, handler, callback)
1111

12-
- `topic` - type: String
13-
- `options` - type: Object, optional, might contain the following properties:
14-
- `discover`: type: Boolean - Will use the DHT to find
15-
16-
`callback` must follow `function (err, subscription) {}` where Subscription is a Node.js Stream in Object mode, emiting a `data` event for each new message on the subscribed topic.`err` is an error if the operation was not successful.
17-
18-
`subscription` has a `.cancel` event in order to cancel the subscription.
12+
- `topic: string`
13+
- `options: Object` - (Optional), might contain the following properties:
14+
- `discover`: type: Boolean - Will use the DHT to find
15+
- `handler: (msg) => ()` - Event handler which will be called with a message object everytime one is received. The `msg` has the format `{from: string, seqno: Buffer, data: Buffer, topicCIDs: Array<string>}`.
16+
- `callback: (Error) => ()` (Optional) Called once the subscription is established.
1917

2018
If no `callback` is passed, a [promise][] is returned.
2119

2220
> _In the future, topic can also be type of TopicDescriptor (https://github.com/libp2p/pubsub-notes/blob/master/flooding/flooding.proto#L23). However, for now, only strings are supported._
2321
22+
#### `pubsub.unsubscribe`
23+
24+
> Unsubscribes from a pubsub topic.
25+
26+
##### `Go` **WIP**
27+
28+
##### `JavaScript` - `ipfs.pubsub.unsubscribe(topic, handler)`
29+
30+
- `topic: string` - The topic to unsubscribe from
31+
- `handler: (msg) => ()` - The handler to remove.
32+
33+
This works like `EventEmitter.removeListener`, as that only the `handler` passed to a `subscribe` call before is removed from listening. The underlying subscription will only be canceled once all listeners for a topic have been removed.
34+
2435
#### `pubsub.publish`
2536

26-
> Publish a data message to a pubsub topic
37+
> Publish a data message to a pubsub topic.
2738
2839
##### `Go` **WIP**
2940

3041
##### `JavaScript` - ipfs.pubsub.publish(topic, data, callback)
3142

32-
- `topic` - type: String
33-
- `data` - type: Buffer
43+
- `topic: string`
44+
- `callback: (Error) => ()` - Calls back with an error or nothing if the publish was successfull.
3445

35-
`callback` must follow `function (err) {}` signature, where `err` is an error if the operation was not successful.
36-
37-
If no `callback` is passed, a [promise][] is returned.
46+
- Returns: `Promise` if no `callback` was passed otherwise `undefined`.
3847

3948
#### `pubsub.ls`
4049

@@ -44,9 +53,10 @@ If no `callback` is passed, a [promise][] is returned.
4453

4554
##### `JavaScript` - ipfs.pubsub.ls(topic, callback)
4655

47-
`callback` must follow `function (err) {}` signature, where `err` is an error if the operation was not successful.
56+
- `topic: string`
57+
- `callback: (Error, Array<string>>) => ()` - Calls back with an error or a list of topicCIDs that this peer is subscribed to.
4858

49-
If no `callback` is passed, a [promise][] is returned.
59+
- Returns: `Promise<Array<string>>` if no `callback` was passed otherwise `undefined`.
5060

5161
#### `pubsub.peers`
5262

@@ -56,8 +66,7 @@ If no `callback` is passed, a [promise][] is returned.
5666

5767
##### `JavaScript` - ipfs.pubsub.peers(topic, callback)
5868

59-
- `topic` - type: String
60-
61-
`callback` must follow `function (err) {}` signature, where `err` is an error if the operation was not successful.
69+
- `topic: string`
70+
- `callback: (Error, Array<string>>) => ()` - Calls back with an error or a list of peer ids subscribed to the `topic`.
6271

63-
If no `callback` is passed, a [promise][] is returned.
72+
- Returns: `Promise<Array<string>>` if no `callback` was passed otherwise `undefined`.

0 commit comments

Comments
 (0)