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

Commit

Permalink
chore: improve docs
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Jan 24, 2019
1 parent 7ca7f06 commit d9734da
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 5 deletions.
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ js-libp2p-pubsub

## Usage

Create your pubsub implementation extending the base protocol.
A pubsub implementation **MUST** overwrite the `_processConnection`, `publish`, `subscribe` and `unsubscribe` functions. `add_peer` and `remove_peer` may be overwritten if the pubsub implementation needs to add custom logic when peers are added and remove. All the remaining functions **MUST NOT** be overwritten.

The following example aims to show how to create your pubsub implementation extending this base protocol. The pubsub implementation will handle the subscriptions logic.

```JavaScript
const Pubsub = require('libp2p-pubsub')
Expand All @@ -43,23 +45,30 @@ class PubsubImplementation extends Pubsub {
}

_processConnection(idB58Str, conn, peer) {
// Required to be implemented by the subclass
// Process each message accordingly
}

publish() {

// Required to be implemented by the subclass
}

subscribe() {

// Required to be implemented by the subclass
}

unsubscribe() {

// Required to be implemented by the subclass
}
}
```

## Implementations using this base protocol

You can use the following implementations as examples for building your own pubsub implementation.

- [libp2p/js-libp2p-floodsub](https://github.com/libp2p/js-libp2p-floodsub)

## Contribute

Feel free to join in. All welcome. Open an [issue](https://github.com/libp2p/js-libp2p-pubsub/issues)!
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"dependencies": {
"async": "^2.6.1",
"debug": "^4.1.1",
"err-code": "^1.1.2",
"length-prefixed-stream": "^1.6.0",
"protons": "^1.0.1",
"pull-pushable": "^2.2.0"
Expand Down
45 changes: 44 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ const EventEmitter = require('events')
const pull = require('pull-stream/pull')
const empty = require('pull-stream/sources/empty')
const asyncEach = require('async/each')

const debug = require('debug')
const errcode = require('err-code')

const Peer = require('./peer')
const message = require('./message')
Expand Down Expand Up @@ -145,8 +147,17 @@ class PubsubBaseProtocol extends EventEmitter {
})
}

/**
* Process connections
* @abstract
* @param {string} idB58Str peer id string in base58
* @param {Connection} conn connection
* @param {PeerInfo} peer peer info
* @returns {undefined}
*
*/
_processConnection (idB58Str, conn, peer) {
throw new Error('_processConnection must be implemented by the subclass')
throw errcode('_processConnection must be implemented by the subclass', 'ERR_NOT_IMPLEMENTED')
}

_onConnectionEnd (idB58Str, peer, err) {
Expand All @@ -159,6 +170,38 @@ class PubsubBaseProtocol extends EventEmitter {
this._removePeer(peer)
}

/**
* Publish messages to the given topics.
* @abstract
* @param {Array<string>|string} topics
* @param {Array<any>|any} messages
* @returns {undefined}
*
*/
publish (topics, messages) {
throw errcode('publish must be implemented by the subclass', 'ERR_NOT_IMPLEMENTED')
}

/**
* Subscribe to the given topic(s).
* @abstract
* @param {Array<string>|string} topics
* @returns {undefined}
*/
subscribe (topics) {
throw errcode('subscribe must be implemented by the subclass', 'ERR_NOT_IMPLEMENTED')
}

/**
* Unsubscribe from the given topic(s).
* @abstract
* @param {Array<string>|string} topics
* @returns {undefined}
*/
unsubscribe (topics) {
throw errcode('unsubscribe must be implemented by the subclass', 'ERR_NOT_IMPLEMENTED')
}

/**
* Mounts the pubsub protocol onto the libp2p node and sends our
* subscriptions to every peer conneceted
Expand Down
12 changes: 12 additions & 0 deletions test/pubsub.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ class PubsubImplementation extends PubsubBaseProtocol {
super('libp2p:pubsub', 'libp2p:pubsub-implementation', libp2p)
}

publish (topics, messages) {
// ...
}

subscribe (topics) {
// ...
}

unsubscribe (topics) {
// ...
}

_processConnection (idB58Str, conn, peer) {
// ...
}
Expand Down

0 comments on commit d9734da

Please sign in to comment.