Skip to content

Commit

Permalink
fix: update to async iterators (#73)
Browse files Browse the repository at this point in the history
* chore: update to latest js-IPFS rc

* refactor: updates to latest async iterator version of ipfs and libp2p

Needs `ipfs.id` to return a `CID` before tests will pass.

* chore: only build master branch and prs

* refactor: ipfs will return peer ids as strings instead

* refactor: remove ipfs and just use libp2p

* fix: fix linting
  • Loading branch information
achingbrain authored Jan 24, 2020
1 parent f4f8ebd commit b8556ec
Show file tree
Hide file tree
Showing 18 changed files with 449 additions and 557 deletions.
37 changes: 37 additions & 0 deletions .aegir.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict'

const Libp2p = require('libp2p')
const PeerInfo = require('peer-info')
const { config } = require('./test/utils/create-libp2p')

let relay

module.exports = {
hooks: {
pre: async () => {
const peerInfo = await PeerInfo.create()
peerInfo.multiaddrs.add('/ip4/127.0.0.1/tcp/24642/ws')

const defaultConfig = await config()

relay = new Libp2p({
...defaultConfig,
peerInfo,
config: {
...defaultConfig.config,
relay: {
enabled: true,
hop: {
enabled: true
}
}
}
})

await relay.start()
},
post: async () => {
await relay.stop()
}
}
}
44 changes: 44 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
language: node_js
branches:
only:
- master
- /^release\/.*$/
cache: npm
stages:
- check
- test
- cov

node_js:
- '12'

os:
- linux
- osx
- windows

script: npx nyc -s npm run test:node -- --bail
after_success: npx nyc report --reporter=text-lcov > coverage.lcov && npx codecov

jobs:
include:
- stage: check
script:
- npx aegir commitlint --travis
- npx aegir dep-check
- npm run lint

- stage: test
name: chrome
addons:
chrome: stable
script: npx aegir test -t browser -t webworker

- stage: test
name: firefox
addons:
firefox: latest
script: npx aegir test -t browser -t webworker -- --browsers FirefoxHeadless

notifications:
email: false
77 changes: 39 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@
[![made by Protocol Labs](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](https://protocol.ai)
[![Freenode](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs)

[![Build Status](https://travis-ci.org/ipfs-shipyard/ipfs-pubsub-room.svg?branch=master)](https://travis-ci.org/ipfs-shipyard/ipfs-pubsub-room)
[![Build Status](https://travis-ci.com/ipfs-shipyard/ipfs-pubsub-room.svg?branch=master)](https://travis-ci.com/ipfs-shipyard/ipfs-pubsub-room)

> Creates a room based on an IPFS pub-sub channel. Emits membership events, listens for messages, broadcast and direct messeges to peers.
> Creates a room based on a LibP2P pub-sub channel. Emits membership events, listens for messages, broadcast and direct messeges to peers.
([Demo video](https://t.co/HNYQGE4D4P))

## js-ipfs

This package has been tested with js-ipfs version __0.32.0__.

## Install

```bash
Expand All @@ -21,59 +17,64 @@ $ npm install ipfs-pubsub-room

## Use

Creating a pubsub room from a LibP2P node

```js
const Room = require('ipfs-pubsub-room')
const Libp2p = require('libp2p')

const libp2p = new Libp2p({ ... })
await libp2p.start()

// libp2p node is ready, so we can start using ipfs-pubsub-room
const room = Room(libp2p, 'room-name')
```

Creating a pubsub room from an IPFS node

```js
const Room = require('ipfs-pubsub-room')
const IPFS = require('ipfs')
const ipfs = new IPFS({
EXPERIMENTAL: {
pubsub: true
},
config: {
Addresses: {
Swarm: [
'/dns4/ws-star.discovery.libp2p.io/tcp/443/wss/p2p-websocket-star'
]
}
}
})

// IPFS node is ready, so we can start using ipfs-pubsub-room
ipfs.on('ready', () => {
const room = Room(ipfs, 'room-name')
const ipfs = await IPFS.create({ ... })
const room = Room(ipfs.libp2p, 'room-name')
```

Once we have a room we can listen for messages

room.on('peer joined', (peer) => {
console.log('Peer joined the room', peer)
})
```js
room.on('peer joined', (peer) => {
console.log('Peer joined the room', peer)
})

room.on('peer left', (peer) => {
console.log('Peer left...', peer)
})
room.on('peer left', (peer) => {
console.log('Peer left...', peer)
})

// now started to listen to room
room.on('subscribed', () => {
console.log('Now connected!')
})
// now started to listen to room
room.on('subscribed', () => {
console.log('Now connected!')
})
```

## API

### Room (ipfs:IPFS, roomName:string, options:object)
### Room (libp2p:LibP2P, roomName:string, options:object)

* `ipfs`: IPFS object. Must have pubsub activated
* `libp2p`: LibP2P node. Must have pubsub activated
* `roomName`: string, global identifier for the room
* `options`: object:
* `pollInterval`: interval for polling the pubsub peers, in ms. Defaults to 1000.

```js
const room = Room(ipfs, 'some-room-name')
const room = Room(libp2p, 'some-room-name')
```

## room.broadcast(message)

Broacasts message (string or buffer).

## room.sendTo(peer, message)
## room.sendTo(cid, message)

Sends message (string or buffer) to peer.

Expand All @@ -85,7 +86,7 @@ Leaves room, stopping everything.

Returns an array of peer identifiers (strings).

## room.hasPeer(peer)
## room.hasPeer(cid)

Returns a boolean indicating if the given peer is present in the room.

Expand All @@ -96,11 +97,11 @@ Listens for messages. A `message` is an object containing the following properti
* `from` (string): peer id
* `data` (Buffer): message content

## room.on('peer joined', (peer) => {})
## room.on('peer joined', (cid) => {})

Once a peer has joined the room.

## room.on('peer left', (peer) => {})
## room.on('peer left', (cid) => {})

Once a peer has left the room.

Expand Down
15 changes: 0 additions & 15 deletions circle.yml

This file was deleted.

22 changes: 12 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"lint": "aegir lint",
"test": "aegir test",
"test:node": "aegir test -t node",
"test:browser": "aegir test - browser"
"test:browser": "aegir test -t browser"
},
"repository": {
"type": "git",
Expand All @@ -28,18 +28,20 @@
"homepage": "https://github.com/ipfs-shipyard/ipfs-pubsub-room#readme",
"dependencies": {
"hyperdiff": "^2.0.5",
"lodash.clonedeep": "^4.5.0",
"pull-pushable": "^2.2.0",
"pull-stream": "^3.6.9"
"it-pipe": "^1.1.0",
"lodash.clonedeep": "^4.5.0"
},
"devDependencies": {
"async": "^2.6.1",
"aegir": "^20.5.1",
"chai": "^4.2.0",
"delay": "^4.3.0",
"dirty-chai": "^2.0.1",
"aegir": "^18.0.3",
"ipfs": "~0.34.4"
},
"browser": {
"./test/utils/create-repo-node.js": "./test/utils/create-repo-browser.js"
"libp2p": "0.27.0-rc.0",
"libp2p-gossipsub": "0.2.1",
"libp2p-mplex": "^0.9.3",
"libp2p-secio": "^0.12.2",
"libp2p-websockets": "^0.13.2",
"peer-info": "^0.17.1",
"rimraf": "^3.0.0"
}
}
Loading

0 comments on commit b8556ec

Please sign in to comment.