Files
server/io.js
2025-04-22 16:15:14 -04:00

41 lines
1.1 KiB
JavaScript

import {createServer} from "http";
import {Server} from "socket.io";
import { createAdapter } from "@socket.io/redis-adapter";
import {redis} from "./cache.js";
import {fileURLToPath} from "url";
import path from "path";
import express from "express";
import {engine} from "express-handlebars";
import {initSnapShare} from "./snapshare.js";
import cors from "cors";
const socketIoOptions = {
}
if( process.env.DEXORDER_APP_URL )
socketIoOptions['cors'] = {origin:process.env.DEXORDER_APP_URL}
// Setup Express
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
app.engine('handlebars', engine({
defaultLayout: false,
}
));
app.set('view engine', 'handlebars');
app.set('views', path.join(__dirname, 'views')); // Set the views directory
app.use(express.static(path.join(__dirname, 'public')));
app.use(cors())
initSnapShare(app)
export const httpServer = createServer(app)
export const io = new Server(httpServer, socketIoOptions)
const pubClient = redis.duplicate();
await pubClient.connect()
const adapter = createAdapter(pubClient, redis, {/*key:'socket.io'*/})
io.adapter(adapter)