|
66 | 66 | * pin.rm
|
67 | 67 | * [ping](#ping) TODO: add docs
|
68 | 68 | * [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) |
74 | 74 | * refs
|
75 | 75 | * refsPullStream
|
76 | 76 | * refs.local
|
@@ -561,3 +561,197 @@ console.log(data.toString('utf8'))
|
561 | 561 | hello world!
|
562 | 562 | */
|
563 | 563 | ```
|
| 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 | +``` |
0 commit comments