2022-10-01 08:57:34 +00:00
|
|
|
import { uniqueNamesGenerator, adjectives, animals } from 'unique-names-generator'
|
|
|
|
import { Awareness } from 'y-protocols/awareness'
|
|
|
|
import { WebrtcProvider } from 'y-webrtc'
|
|
|
|
import * as Y from 'yjs'
|
2022-10-04 10:07:44 +00:00
|
|
|
// import type { Reaction } from '../graphql/types.gen'
|
2022-10-01 08:57:34 +00:00
|
|
|
|
2022-10-04 10:07:44 +00:00
|
|
|
export const roomConnect = (room, keyname = 'reactions'): [Y.XmlFragment, WebrtcProvider] => {
|
2022-10-01 08:57:34 +00:00
|
|
|
const ydoc = new Y.Doc()
|
2022-10-04 10:07:44 +00:00
|
|
|
const yxmlfrag = ydoc.getXmlFragment(keyname) // TODO: encode/decode payload to Reactions[]
|
2022-10-01 08:57:34 +00:00
|
|
|
const webrtcOptions = {
|
|
|
|
awareness: new Awareness(ydoc),
|
|
|
|
filterBcConns: true,
|
|
|
|
maxConns: 33,
|
|
|
|
signaling: [
|
|
|
|
// 'wss://signaling.discours.io',
|
|
|
|
// 'wss://stun.l.google.com:19302',
|
|
|
|
'wss://y-webrtc-signaling-eu.herokuapp.com',
|
|
|
|
'wss://signaling.yjs.dev'
|
|
|
|
],
|
|
|
|
peerOpts: {},
|
|
|
|
password: ''
|
|
|
|
}
|
|
|
|
const provider = new WebrtcProvider(room, ydoc, webrtcOptions)
|
|
|
|
const username = uniqueNamesGenerator({
|
|
|
|
dictionaries: [adjectives, animals],
|
|
|
|
style: 'capital',
|
|
|
|
separator: ' ',
|
|
|
|
length: 2
|
|
|
|
})
|
|
|
|
|
|
|
|
provider.awareness.setLocalStateField('user', {
|
|
|
|
name: username
|
|
|
|
})
|
2022-10-04 10:07:44 +00:00
|
|
|
return [yxmlfrag, provider]
|
2022-10-01 08:57:34 +00:00
|
|
|
}
|