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 | ## js-ipfs
|
13 |
|
14 | This package has been tested with js-ipfs version __0.34.4__.
|
15 |
|
16 | ## Install
|
17 |
|
18 | ```bash
|
19 | $ npm install ipfs-pubsub-room
|
20 | ```
|
21 |
|
22 | ## Use
|
23 |
|
24 | ```js
|
25 | const Room = require('ipfs-pubsub-room')
|
26 | const IPFS = require('ipfs')
|
27 | const ipfs = new IPFS({
|
28 | EXPERIMENTAL: {
|
29 | pubsub: true
|
30 | },
|
31 | config: {
|
32 | Addresses: {
|
33 | Swarm: [
|
34 | '/dns4/ws-star.discovery.libp2p.io/tcp/443/wss/p2p-websocket-star'
|
35 | ]
|
36 | }
|
37 | }
|
38 | })
|
39 |
|
40 | // IPFS node is ready, so we can start using ipfs-pubsub-room
|
41 | ipfs.on('ready', () => {
|
42 | const room = Room(ipfs, 'room-name')
|
43 |
|
44 | room.on('peer joined', (peer) => {
|
45 | console.log('Peer joined the room', peer)
|
46 | })
|
47 |
|
48 | room.on('peer left', (peer) => {
|
49 | console.log('Peer left...', peer)
|
50 | })
|
51 |
|
52 | // now started to listen to room
|
53 | room.on('subscribed', () => {
|
54 | console.log('Now connected!')
|
55 | })
|
56 | })
|
57 | ```
|
58 |
|
59 | ## API
|
60 |
|
61 | ### Room (ipfs:IPFS, roomName:string, options:object)
|
62 |
|
63 | * `ipfs`: IPFS object. 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
|
69 | const room = Room(ipfs, 'some-room-name')
|
70 | ```
|
71 |
|
72 | ## room.broadcast(message)
|
73 |
|
74 | Broacasts message (string or buffer).
|
75 |
|
76 | ## room.sendTo(peer, message)
|
77 |
|
78 | Sends message (string or buffer) to peer.
|
79 |
|
80 | ## async room.leave()
|
81 |
|
82 | Leaves room, stopping everything.
|
83 |
|
84 | ## room.getPeers()
|
85 |
|
86 | Returns an array of peer identifiers (strings).
|
87 |
|
88 | ## room.hasPeer(peer)
|
89 |
|
90 | Returns a boolean indicating if the given peer is present in the room.
|
91 |
|
92 | ## room.on('message', (message) => {})
|
93 |
|
94 | Listens 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', (peer) => {})
|
100 |
|
101 | Once a peer has joined the room.
|
102 |
|
103 | ## room.on('peer left', (peer) => {})
|
104 |
|
105 | Once a peer has left the room.
|
106 |
|
107 | ## room.on('subscribed',() => {})
|
108 |
|
109 | Once your program has subscribed the topic and announced through IPFS pubsub.
|
110 |
|
111 | ## License
|
112 |
|
113 | ISC
|