Skip to content
This repository was archived by the owner on Jun 30, 2023. It is now read-only.

Commit b045238

Browse files
author
Alan Shaw
authored
feat: pubsub (#1)
* feat: pubsub * chore: appease linter * fix: remove .only * fix: remove ua headre change - cannot in browsers * feat: add pubsub example * feat: enter to submit * fix: add timeout * fix: temporarily disable firefox testing
1 parent f30e261 commit b045238

33 files changed

+723
-121
lines changed

.travis.yml

-6
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,5 @@ jobs:
3434
chrome: stable
3535
script: npx aegir test -t browser -t webworker
3636

37-
- stage: test
38-
name: firefox
39-
addons:
40-
firefox: latest
41-
script: npx aegir test -t browser -t webworker -- --browsers FirefoxHeadless
42-
4337
notifications:
4438
email: false

API.md

+199-5
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@
6666
* pin.rm
6767
* [ping](#ping) TODO: add docs
6868
* [pingPullStream](#pingpullstream) TODO: add docs
69-
* pubsub.publish
70-
* pubsub.ls
71-
* pubsub.peers
72-
* pubsub.subscribe
73-
* pubsub.unsubscribe
69+
* [pubsub.ls](#pubsubls)
70+
* [pubsub.peers](#pubsubpeers)
71+
* [pubsub.publish](#pubsubpublish)
72+
* [pubsub.subscribe](#pubsubsubscribe)
73+
* [pubsub.unsubscribe](#pubsubunsubscribe)
7474
* refs
7575
* refsPullStream
7676
* refs.local
@@ -561,3 +561,197 @@ console.log(data.toString('utf8'))
561561
hello world!
562562
*/
563563
```
564+
565+
## pubsub.ls
566+
567+
List subscribed topics by name.
568+
569+
### `pubsub.ls([options]): Promise<String[]>`
570+
571+
#### Parameters
572+
573+
* `options` (optional)
574+
* Type: `Object`
575+
* Default: `null`
576+
* `options.signal` (optional) - A signal that can be used to abort the request
577+
* Type: [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal)
578+
* Default: `null`
579+
580+
#### Returns
581+
582+
An array of subscribed topic names.
583+
584+
* Type: `Promise<String[]>`
585+
586+
#### Examples
587+
588+
```js
589+
const res = await ipfs.pubsub.ls()
590+
console.log(res)
591+
/*
592+
[ 'my-pubsub-topic' ]
593+
*/
594+
```
595+
596+
## pubsub.peers
597+
598+
List peers we are currently pubsubbing with, optionally filtered by topic name.
599+
600+
### `pubsub.peers([topic], [options]): Promise<String[]>`
601+
602+
#### Parameters
603+
604+
* `topic` (optional) - Pubsub topic name to filter peer list by
605+
* Type: `String`
606+
* Default: `null`
607+
* `options` (optional)
608+
* Type: `Object`
609+
* Default: `null`
610+
* `options.signal` (optional) - A signal that can be used to abort the request
611+
* Type: [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal)
612+
* Default: `null`
613+
614+
#### Returns
615+
616+
An array of string peer IDs.
617+
618+
* Type: `Promise<String[]>`
619+
620+
#### Examples
621+
622+
```js
623+
const res = await ipfs.pubsub.peers()
624+
console.log(res)
625+
/*
626+
[ 'QmPefeutipT4odZHRyBE3xBcWQxmBxZqS7n5zQxKZP9TNp' ]
627+
*/
628+
```
629+
630+
## pubsub.publish
631+
632+
Publish a message to a given pubsub topic.
633+
634+
### `pubsub.publish(topic, message, [options]): Promise`
635+
636+
#### Parameters
637+
638+
* `topic` - Pubsub topic name to publish the topic to
639+
* Type: `String`
640+
* `message` - Message to publish
641+
* Type: `Buffer`/`ArrayBuffer`/`String`
642+
* `options` (optional)
643+
* Type: `Object`
644+
* Default: `null`
645+
* `options.signal` (optional) - A signal that can be used to abort the request
646+
* Type: [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal)
647+
* Default: `null`
648+
649+
#### Returns
650+
651+
`Promise` resolved when the message has been published.
652+
653+
* Type: `Promise`
654+
655+
#### Examples
656+
657+
```js
658+
await ipfs.pubsub.publish('my-pubsub-topic', Buffer.from('hello world!'))
659+
```
660+
661+
## pubsub.subscribe
662+
663+
Subscribe to messages on a given topic.
664+
665+
**Note that in the browser there is a per-domain open request limit (6 for most browsers)**
666+
667+
### `pubsub.subscribe(topic, handler, [options]): Promise`
668+
669+
#### Parameters
670+
671+
* `topic` - Pubsub topic name to subscribe to messages for
672+
* Type: `String`
673+
* `handler` - A function called every time this node receives a message for the given topic.
674+
* Type: `Function(msg<Object>)`. Message properties:
675+
* `from` - Peer ID of the peer this message came from
676+
* Type: `Buffer`
677+
* `data` - Raw message data
678+
* Type: `Buffer`
679+
* `seqno` - 20 byte random message number
680+
* Type: `Buffer`
681+
* `topicIDs` - Topic names this message was published to
682+
* Type: `String[]`
683+
* `options` (optional)
684+
* Type: `Object`
685+
* Default: `null`
686+
* `options.discover` (optional) - Try to discover other peers subscribed to the same topic
687+
* Type: `Boolean`
688+
* Deafult: `false`
689+
* `options.onError` (optional) - An error handler called when the request errors or parsing of a given message fails. It is passed two parameters, the error that occurred and a boolean indicating if it was a fatal error or not (fatal errors terminate the subscription).
690+
* Type: `Function(err<Error>, fatal<Boolean>)`
691+
* Default: `null`
692+
* `options.signal` (optional) - A signal that can be used to abort the request
693+
* Type: [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal)
694+
* Default: `null`
695+
696+
#### Returns
697+
698+
`Promise` resolved when initial subscription has been set up.
699+
700+
* Type: `Promise`
701+
702+
#### Examples
703+
704+
```js
705+
await ipfs.pubsub.subscribe('my-pubsub-topic', msg => {
706+
console.log(msg)
707+
console.log('data: ', msg.data.toString())
708+
})
709+
/*
710+
{
711+
from: <Buffer 12 20 70 c6 f4 37 4d 49 d2 7f 3a 26 fd 3c 91 ac 15 40 57 f5 93 2d 96 2b ec 1b ce b5 76 10 0c 54 f8 ad>,
712+
data: <Buffer 68 69>,
713+
seqno: <Buffer 15 af 62 bb 78 af 86 79>,
714+
topicIDs: [ 'my-pubsub-topic' ]
715+
}
716+
data: hi
717+
*/
718+
```
719+
720+
## pubsub.unsubscribe
721+
722+
Stop receiving messages for a given topic.
723+
724+
### `pubsub.unsubscribe(topic, [handler], [options]): Promise`
725+
726+
#### Parameters
727+
728+
* `topic` - Pubsub topic name to unsubscribe from.
729+
* Type: `String`
730+
* `handler` (optional) - The handler function currently registered for this topic. If not provided, **all** handlers for the passed topic will be unsubscribed. Note this only works using the Promise API.
731+
* Type: `Function`
732+
* Default: `null`
733+
* `options` (optional)
734+
* Type: `Object`
735+
* Default: `null`
736+
* `options.signal` (optional) - A signal that can be used to abort the request
737+
* Type: [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal)
738+
* Default: `null`
739+
740+
#### Returns
741+
742+
`Promise` resolved when topic has been unsubscribed.
743+
744+
* Type: `Promise`
745+
746+
#### Examples
747+
748+
```js
749+
const handler = msg => console.log(msg)
750+
await ipfs.pubsub.unsubscribe('my-pubsub-topic', handler)
751+
```
752+
753+
Unsubscribe all handlers:
754+
755+
```js
756+
await ipfs.pubsub.unsubscribe('my-pubsub-topic')
757+
```

README.md

+7-8
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,11 @@ Instead of a local installation (and bundling) you may request a remote copy of
102102
To always request the latest version, use the following:
103103

104104
```html
105-
<!-- loading the minified version -->
106105
<script src="https://unpkg.com/ipfs-http-client-lite/dist/index.min.js"></script>
107-
<!-- loading the human-readable (not minified) version -->
108-
<script src="https://unpkg.com/ipfs-http-client-lite/dist/index.js"></script>
109106
```
110107

108+
You can also use the un-minified version, just remove ".min" from the URL.
109+
111110
For maximum security you may also decide to:
112111

113112
* Reference a specific version of the IPFS HTTP API client (to prevent unexpected breaking changes when a newer latest version is published)
@@ -204,11 +203,11 @@ This module is in heavy development, not all API methods are available (or docum
204203
* pin.rm
205204
* [ping](./API.md#ping) TODO: add docs
206205
* [pingPullStream](./API.md#pingpullstream) TODO: add docs
207-
* pubsub.publish
208-
* pubsub.ls
209-
* pubsub.peers
210-
* pubsub.subscribe
211-
* pubsub.unsubscribe
206+
* [pubsub.ls](./API.md#pubsubls)
207+
* [pubsub.peers](./API.md#pubsubpeers)
208+
* [pubsub.publish](./API.md#pubsubpublish)
209+
* [pubsub.subscribe](./API.md#pubsubsubscribe)
210+
* [pubsub.unsubscribe](./API.md#pubsubunsubscribe)
212211
* refs
213212
* refsPullStream
214213
* refs.local

0 commit comments

Comments
 (0)