File size: 1,295 Bytes
c904036
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
 * Socket.IO Service for bypassing CORS issues
 * This service provides a WebSocket-based API that doesn't have CORS restrictions
 */

const { logger } = require('../Middleware/errorHandler');

class SocketService {
  constructor(io) {
    this.io = io;
  }

  /**
   * Send real-time notification to a specific user
   */
  sendNotificationToUser(userId, notification) {
    try {
      this.io.to(`user:${userId}`).emit('notification', notification);
      logger.info(`Notification sent to user ${userId}`);
    } catch (error) {
      logger.error('Error sending notification:', error);
    }
  }

  /**
   * Broadcast message to all connected clients
   */
  broadcast(event, data) {
    try {
      this.io.emit(event, data);
      logger.info(`Broadcasted event: ${event}`);
    } catch (error) {
      logger.error('Error broadcasting:', error);
    }
  }

  /**
   * Send message to specific room
   */
  sendToRoom(room, event, data) {
    try {
      this.io.to(room).emit(event, data);
      logger.info(`Sent ${event} to room ${room}`);
    } catch (error) {
      logger.error('Error sending to room:', error);
    }
  }

  /**
   * Get connected clients count
   */
  getConnectedClientsCount() {
    return this.io.engine.clientsCount;
  }
}

module.exports = SocketService;