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

Commit 99a6bc4

Browse files
authored
Merge pull request #101 from ipfs/tests/pubsub
feature: add pubsub (tests and API Spec)
2 parents 7bfec31 + 142c417 commit 99a6bc4

File tree

3 files changed

+650
-0
lines changed

3 files changed

+650
-0
lines changed

API/pubsub/README.md

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
pubsub API
2+
==========
3+
4+
#### `pubsub.subscribe`
5+
6+
> Subscribe to a pubsub topic.
7+
8+
##### `Go` **WIP**
9+
10+
##### `JavaScript` - ipfs.pubsub.subscribe(topic, options, handler, callback)
11+
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.
17+
18+
If no `callback` is passed, a [promise][] is returned.
19+
20+
> _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._
21+
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+
35+
#### `pubsub.publish`
36+
37+
> Publish a data message to a pubsub topic.
38+
39+
##### `Go` **WIP**
40+
41+
##### `JavaScript` - ipfs.pubsub.publish(topic, data, callback)
42+
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.
46+
47+
If no `callback` is passed, a promise is returned.
48+
49+
#### `pubsub.ls`
50+
51+
> Returns the list of subscriptions the peer is subscribed to.
52+
53+
##### `Go` **WIP**
54+
55+
##### `JavaScript` - ipfs.pubsub.ls(topic, callback)
56+
57+
- `topic: string`
58+
- `callback: (Error, Array<string>>) => ()` - Calls back with an error or a list of topicCIDs that this peer is subscribed to.
59+
60+
If no `callback` is passed, a promise is returned.
61+
62+
#### `pubsub.peers`
63+
64+
> Returns the peers that are subscribed to one topic.
65+
66+
##### `Go` **WIP**
67+
68+
##### `JavaScript` - ipfs.pubsub.peers(topic, callback)
69+
70+
- `topic: string`
71+
- `callback: (Error, Array<string>>) => ()` - Calls back with an error or a list of peer ids subscribed to the `topic`.
72+
73+
If no `callback` is passed, a promise is returned.

src/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ exports.generic = require('./generic')
88
exports.swarm = require('./swarm')
99
exports.block = require('./block')
1010
exports.dht = require('./dht')
11+
exports.pubsub = require('./pubsub')

0 commit comments

Comments
 (0)