const express = require('express'); const http = require('http'); const { Server } = require('socket.io'); const compression = require('compression'); const events = Object.fromEntries( [ 'authenticate', 'create_thread', 'create_user', 'get_history', 'get_keys', 'get_space', 'list_threads', 'save_span', 'send_message' ].map(event => [event, require('./src/' + event)]) ); const app = express(); app.use(compression()); const server = http.createServer(app); const io = new Server(server, { cors: { origin: true, }, }); const PORT = process.env.PORT || 3435; io.cache = {}; io.on('connection', (socket) => { for (let event in events) { socket.on(event, (msg, callback) => events[event](msg, callback, socket, io) ); } socket.on('disconnect', reason => { let sockets = io.cache[socket.username]; if (sockets) sockets.splice(sockets.indexOf(socket.id), 1); }) }); server.listen(PORT, () => { console.log('server running on port ' + PORT); }); app.use(express.static('client'));