// src/socket/socketServer.js
const { Server } = require('socket.io');
module.exports = (httpServer) => {
const io = new Server(httpServer, {
cors: { origin: '*' }
});
io.on('connection', (socket) => {
console.log('Socket connected:', socket.id);
socket.on('joinChannel', (channel) => {
socket.join(channel);
console.log(`Socket ${socket.id} joined channel: ${channel}`);
});
socket.on('chatMessage', ({ channel, message }) => {
io.to(channel).emit('chatMessage', {
senderId: socket.id,
message,
timestamp: new Date()
});
});
socket.on('disconnect', () => {
console.log('Socket disconnected:', socket.id);
});
});
};