UNPKG

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