File size: 1,770 Bytes
8a6248c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// socket/appointmentSocket.js
import User from '../models/auth.model.js';

export default function initAppointmentSocket(io) {
    io.on('connection', (socket) => {
        console.log("New user connected with socketId:", socket.id);

        // Event when user sets their socketId
        socket.on('setUser', async (userId) => {
            try {
                const user = await User.findById(userId);

                if (!user) {
                    console.error("User not found!");
                    return;
                }

                // Save socketId in the user document
                user.socketId = socket.id;
                if (!user.email) {
                    console.error("User email is missing, cannot update socketId.");
                    return;
                }

                await user.save();
                console.log(`Socket ID saved for user ${user.email}`);
            } catch (error) {
                console.error("Error saving socketId:", error.message);
            }
        });

        // Event when a user starts an appointment
        socket.on('start-appointment', (data) => {
            const { appointmentId } = data;
            socket.join(appointmentId);
            io.to(appointmentId).emit('appointment-started', { appointmentId });
        });

        // Event when a user joins an appointment
        socket.on('join-appointment', (data) => {
            const { appointmentId } = data;
            socket.join(appointmentId);
            io.to(appointmentId).emit('appointment-joined', { appointmentId });
        });

        // Handle disconnect
        socket.on('disconnect', () => {
            console.log(`User disconnected from appointment socket: ${socket.id}`);
        });
    });
}