Socket
Socket
Sign inDemoInstall

ipfs-pubsub-room

Package Overview
Dependencies
32
Maintainers
2
Versions
32
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    ipfs-pubsub-room

IPFS pub-sub room


Version published
Weekly downloads
125
increased by37.36%
Maintainers
2
Install size
3.03 MB
Created
Weekly downloads
 

Readme

Source

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

Keywords

FAQs

Last updated on 23 Jun 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc