const { v4 } = require('uuid') const crud = require('./crud') const connections = {} const subscribe = (ws, sid) => { const id = v4() connections[id] = { connection: ws, streamid: sid, } ws.send(JSON.stringify({ event: 'info', data: 'Welcome to APL Nuke v1.1.0!', })) sendInitial(id) ws.on('close', () => { delete connections[id] }) } const cModel = { 'events': 'event', 'matches': 'matches', 'casters': 'casters', 'hosts': 'hosts', } const recvUpdate = async (channel, data) => { Object.keys(connections).forEach((k) => { sendInitial(k) }) } const sendInitial = async (id) => { const c = connections[id] if (c.streamid !== null) { const stream = await crud['streams'].getById(c.streamid) c.connection.send(JSON.stringify({ event: 'streams:read', data: stream, })) } } module.exports = { subscribe, recvUpdate, }