aboutsummaryrefslogtreecommitdiffstats
path: root/backend/src/socket/index.js
diff options
context:
space:
mode:
Diffstat (limited to 'backend/src/socket/index.js')
-rw-r--r--backend/src/socket/index.js42
1 files changed, 42 insertions, 0 deletions
diff --git a/backend/src/socket/index.js b/backend/src/socket/index.js
new file mode 100644
index 0000000..0c25ed4
--- /dev/null
+++ b/backend/src/socket/index.js
@@ -0,0 +1,42 @@
+const socketIO = require('socket.io');
+const handlers = require('./handlers');
+
+/**
+ * Initialize Socket.IO
+ * @param {object} server - HTTP server instance
+ * @returns {object} Socket.IO instance
+ */
+const initializeSocket = (server) => {
+ const io = socketIO(server, {
+ cors: {
+ origin: '*',
+ methods: ['GET', 'POST']
+ }
+ });
+
+ // Authentication middleware for socket connections
+ io.use((socket, next) => {
+ // Simple authentication can be added here if needed
+ // const token = socket.handshake.auth.token;
+ // if (token validation logic) next();
+ // else next(new Error('Authentication error'));
+ next();
+ });
+
+ // Handle connection
+ io.on('connection', (socket) => {
+ console.log(`New client connected: ${socket.id}`);
+
+ // Register event handlers
+ handlers.registerHandlers(io, socket);
+
+ // Handle disconnection
+ socket.on('disconnect', () => {
+ console.log(`Client disconnected: ${socket.id}`);
+ });
+ });
+
+ return io;
+};
+
+module.exports = initializeSocket; \ No newline at end of file