-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathchat.js
More file actions
39 lines (30 loc) · 828 Bytes
/
chat.js
File metadata and controls
39 lines (30 loc) · 828 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
const signalhub = require('signalhub')
const ram = require('random-access-memory')
const swarm = require('@geut/discovery-swarm-webrtc')
const saga = require('./')
const webrtcOpts = {}
async function initChat (username, key) {
const publicKey = key && key.length > 0 ? key : null
const chat = saga(ram, publicKey, username)
await chat.initialize()
// (1)
const sw = swarm({
/* id: ... , */
/* stream: () => ... */
})
// (2)
// creating a signalhub instance...
const discoveryKey = /* ... */
const signalUrls = ['https://signalhub-jccqtwhdwc.now.sh/']
sw.join(signalhub(discoveryKey, signalUrls), webrtcOpts)
sw.on('connection', async peer => {
try {
// (3)
/* await ... */
} catch (err) {
console.log(err)
}
})
return chat
}
module.exports = initChat