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

Commit 1a82890

Browse files
dignifiedquiredaviddias
authored andcommitted
feat: new event based API
1 parent d004577 commit 1a82890

File tree

3 files changed

+396
-373
lines changed

3 files changed

+396
-373
lines changed

API/pubsub/README.md

+31-21
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,48 @@ 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 other peers.
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+
- `data: buffer` - The actual message to send
45+
- `callback: (Error) => ()` - Calls back with an error or nothing if the publish was successfull.
3446

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.
47+
If no `callback` is passed, a promise is returned.
3848

3949
#### `pubsub.ls`
4050

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

4555
##### `JavaScript` - ipfs.pubsub.ls(topic, callback)
4656

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

49-
If no `callback` is passed, a [promise][] is returned.
60+
If no `callback` is passed, a promise is returned.
5061

5162
#### `pubsub.peers`
5263

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

5768
##### `JavaScript` - ipfs.pubsub.peers(topic, callback)
5869

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

63-
If no `callback` is passed, a [promise][] is returned.
73+
If no `callback` is passed, a promise is returned.

src/object.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ module.exports = (common) => {
4242
})
4343
})
4444

45-
it.skip('template unixfs-dir', (done) => {
45+
it('template unixfs-dir', (done) => {
4646
ipfs.object.new('unixfs-dir', (err, node) => {
4747
expect(err).to.not.exist
4848
const nodeJSON = node.toJSON()

0 commit comments

Comments
 (0)