Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

ipfs-pubsub-room

ipfs-shipyard301ISC3.0.0

IPFS pub-sub room

ipfs, pubsub, p2p, room, broadcast

readme

ipfs-pubsub-room

made by Protocol Labs Freenode Build Status

Creates a room based on a LibP2P pub-sub channel. Emits membership events, listens for messages, broadcast and direct messeges to peers.

(Demo video)

Install

$ npm install ipfs-pubsub-room

Use

Creating a pubsub room from a LibP2P node

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

const Room = require('ipfs-pubsub-room')
const IPFS = require('ipfs')

const ipfs = await IPFS.create({ ... })
const room = Room(ipfs, 'room-name')

Once we have a room we can listen for messages

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

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

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

API

Room (libp2p:LibP2P, roomName:string, options:object)

  • 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.
const room = Room(libp2p, 'some-room-name')

room.broadcast(message)

Broacasts message (string or Uint8Array).

room.sendTo(cid, message)

Sends message (string or Uint8Array) to peer.

async room.leave()

Leaves room, stopping everything.

room.getPeers()

Returns an array of peer identifiers (strings).

room.hasPeer(cid)

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

room.on('message', (message) => {})

Listens for messages. A message is an object containing the following properties:

  • from (string): peer id
  • data (Uint8Array): message content

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

Once a peer has joined the room.

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

Once a peer has left the room.

room.on('subscribed',() => {})

Once your program has subscribed the topic and announced through IPFS pubsub.

License

ISC

changelog

2.0.2 (2022-06-23)

Bug Fixes

  • remove buffers for better browser compat (#83) (5a85d0a)

2.0.1 (2020-01-28)

Bug Fixes

  • support old constructor (c742cd9)

2.0.0 (2020-01-24)

Bug Fixes

Features

1.2.1 (2018-05-03)

1.2.0 (2018-03-12)

Bug Fixes

  • leave room before ending test (f91cffc)

1.1.5 (2017-12-30)

Bug Fixes

  • leave stops peer poller (74d57da)

1.1.4 (2017-12-15)

1.1.3 (2017-12-12)

Bug Fixes

  • handling not connecting by emitting disconnect event. Should fix #2 (2a1b2b7)

1.1.2 (2017-12-07)

Bug Fixes

  • sending messages after connection (e050c27)

1.1.1 (2017-12-07)

Bug Fixes

1.1.0 (2017-12-05)

Bug Fixes

  • not emitting errors when network error occurs (2094577)

1.0.1 (2017-12-05)

Bug Fixes

  • adding a peer to the list of connections so that we can clean up when leaving the room (634b5b8)
  • sendTo uses shared connection - part 2: making sure messages get delivered to the correct rooms by using a global handler. Should fix #25 (88f2ff0)

1.0.0 (2017-12-04)

Bug Fixes

  • sendTo uses shared connection: making sure messages get delivered to the correct rooms. Should fix #25 (2edf576)
  • sendTo uses shared connection: making sure messages get delivered to the correct rooms. Should fix #25 (6d091d0)

0.6.1 (2017-11-21)

Bug Fixes

  • producing the pubsub message field topicIDs, which was renamed from topicCIDs (d55839d)

0.5.1 (2017-11-10)

Bug Fixes

0.5.0 (2017-11-03)

0.4.2 (2017-11-03)

0.4.1 (2017-10-30)

Bug Fixes

  • Direct message's shape to match pubsub messages (#16) (b9bd6f3)

0.4.0 (2017-10-20)

0.3.0 (2017-09-26)

0.2.0 (2017-09-01)

0.1.5 (2017-08-23)

0.1.4 (2017-08-16)

0.1.3 (2017-07-14)

0.1.2 (2017-06-23)

0.1.1 (2017-06-17)

0.1.0 (2017-06-17)