UNPKG

2.37 kBMarkdownView Raw
1# ipfs-pubsub-room
2
3[![made by Protocol Labs](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](https://protocol.ai)
4[![Freenode](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs)
5
6[![Build Status](https://travis-ci.org/ipfs-shipyard/ipfs-pubsub-room.svg?branch=master)](https://travis-ci.org/ipfs-shipyard/ipfs-pubsub-room)
7
8> Creates a room based on an IPFS pub-sub channel. Emits membership events, listens for messages, broadcast and direct messeges to peers.
9
10([Demo video](https://t.co/HNYQGE4D4P))
11
12## Install
13
14```bash
15$ npm install ipfs-pubsub-room
16```
17
18## Use
19
20```js
21const Room = require('ipfs-pubsub-room')
22const IPFS = require('ipfs')
23const ipfs = new IPFS({
24 EXPERIMENTAL: {
25 pubsub: true
26 }
27})
28
29// IPFS node is ready, so we can start using ipfs-pubsub-room
30ipfs.on('ready', () => {
31 const room = Room(ipfs, 'room-name')
32
33 room.on('peer joined', (peer) => {
34 console.log('Peer joined the room', peer)
35 })
36
37 room.on('peer left', (peer) => {
38 console.log('Peer left...', peer)
39 })
40
41 // now started to listen to room
42 room.on('subscribed', () => {
43 console.log('Now connected!')
44 })
45})
46```
47
48## API
49
50### Room (ipfs:IPFS, roomName:string, options:object)
51
52* `ipfs`: IPFS object. Must have pubsub activated
53* `roomName`: string, global identifier for the room
54* `options`: object:
55 * `pollInterval`: interval for polling the pubsub peers, in ms. Defaults to 1000.
56
57```js
58const room = Room(ipfs, 'some-room-name')
59```
60
61## room.broadcast(message)
62
63Broacasts message (string or buffer).
64
65## room.sendTo(peer, message)
66
67Sends message (string or buffer) to peer.
68
69## room.leave()
70
71Leaves room, stopping everything.
72
73## room.getPeers()
74
75Returns an array of peer identifiers (strings).
76
77## room.hasPeer(peer)
78
79Returns a boolean indicating if the given peer is present in the room.
80
81## room.on('message', (message) => {})
82
83Listens for messages. A `message` is an object containing the following properties:
84
85* `from` (string): peer id
86* `data` (Buffer): message content
87
88## room.on('peer joined', (peer) => {})
89
90Once a peer has joined the room.
91
92## room.on('peer left', (peer) => {})
93
94Once a peer has left the room.
95
96## room.on('subscribed',() => {})
97
98Once your program has subscribed the topic and announced through IPFS pubsub.
99
100## License
101
102ISC