samoulla-backend / utils /socket.js
Samoulla Sync Bot
Auto-deploy Samoulla Backend: b68e45770de26ed39feb4b1c0925e5345eb3a61d
634b9bb
const socketIo = require('socket.io');
let io;
const init = (server) => {
io = socketIo(server, {
cors: {
methods: ['GET', 'POST'],
credentials: true,
},
});
io.on('connection', (socket) => {
console.log('New client connected');
socket.on('join', (userId) => {
console.log(`User ${userId} joined their own room`);
socket.join(userId);
});
socket.on('disconnect', () => {
console.log('Client disconnected');
});
});
return io;
};
const getIO = () => {
if (!io) {
throw new Error('Socket.io not initialized!');
}
return io;
};
module.exports = {
init,
getIO,
};