2.3.3.1 Socket.io Setup

// backend/src/socket/chatSocket.js
module.exports = (io) => {
  io.on('connection', (socket) => {
    console.log('[Chat] New connection:', socket.id);

    // Join a specific chat channel (coin-based, team-based, or direct)
    socket.on('joinChannel', (channelName) => {
      socket.join(channelName);
      console.log(`[Chat] Socket ${socket.id} joined: ${channelName}`);
    });

    // Handle incoming messages
    socket.on('chatMessage', ({ channel, content }) => {
      // Broadcast to everyone in channel
      io.to(channel).emit('chatMessage', {
        senderId: socket.id,
        content,
        timestamp: Date.now()
      });
    });

    // Clean up on disconnect
    socket.on('disconnect', () => {
      console.log('[Chat] Socket disconnected:', socket.id);
    });
  });
};

Integration within the main socketServer.js:

// backend/src/socket/socketServer.js
const { Server } = require('socket.io');
const chatSocket = require('./chatSocket');

module.exports = (httpServer) => {
  const io = new Server(httpServer, { cors: { origin: '*' } });
  
  // Pass io to each feature-specific module
  chatSocket(io);
  
  // Potentially also integrate separate modules for trade signals, presence, etc.
  
  return io;
};

Last updated