-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
203 lines (170 loc) · 6.11 KB
/
index.js
File metadata and controls
203 lines (170 loc) · 6.11 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import Fastify from "fastify";
import fastifyCors from "@fastify/cors";
import { Server as Io } from "socket.io";
import { dbConnect } from "./src/config/db.js";
import { taskRoutes } from "./src/routes/task.route.js";
import dotenv from "dotenv";
import { authRoute } from "./src/routes/user.route.js";
import { authenticate } from "./src/middleware/authmiddleware.js";
import jwt from "jsonwebtoken";
import { commentRoutes } from "./src/routes/comment.route.js";
import { roomRoutes } from "./src/routes/room.route.js";
import notificationRoutes from "./src/routes/notification.route.js";
dotenv.config();
const app = Fastify({
logger: true,
});
// Fonction utilitaire pour vérifier le token JWT
const verifySocketToken = async (token) => {
try {
if (!process.env.JWT_SECRET) {
console.error(
"JWT_SECRET n'est pas défini dans les variables d'environnement"
);
throw new Error("Erreur de configuration du serveur");
}
const decoded = jwt.verify(token, process.env.JWT_SECRET);
if (!decoded || !decoded.userId) {
throw new Error("Token invalide: informations manquantes");
}
return decoded;
} catch (error) {
console.error("Erreur de vérification du token:", error.message);
throw new Error("Token invalide");
}
};
// Middleware pour parser le token JWT
app.decorate("authenticate", authenticate);
async function startServer() {
try {
// Register CORS
await app.register(fastifyCors, {
origin: process.env.FRONTEND_URL || "http://localhost:5173",
methods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
credentials: true,
allowedHeaders: ["Content-Type", "Authorization"],
});
// Connect to database
await dbConnect();
// Initialize Socket.IO AVANT les routes
const io = new Io(app.server, {
cors: {
origin: process.env.FRONTEND_URL || "http://localhost:5173",
credentials: true,
},
});
// Authentification des sockets
io.use(async (socket, next) => {
try {
const token = socket.handshake.auth.token;
if (!token) {
return next(new Error("Token manquant"));
}
const decode = await verifySocketToken(token);
if (!decode || !decode.userId) {
return next(new Error("Token invalide"));
}
socket.userId = decode.userId;
// Rejoint la room de l'utilisateur
socket.join(`user_${socket.userId}`);
console.log(`Socket authenticated for user: ${socket.userId}`);
next();
} catch (err) {
console.log("Socket auth failed:", err.message);
next(new Error("Authenticate failed")); // Permettre les connexions non authentifiées pour certains cas
}
});
// Décorer Fastify avec io pour l'utiliser dans les routes
app.decorate("io", io);
// Socket.IO events
io.on("connection", (socket) => {
console.log(`New client connected: ${socket.id}, User ${socket.userId}`);
// Si l'utilisateur est authentifié, joindre sa room personnelle
if (socket.userId) {
socket.join(`user_${socket.userId}`);
console.log(`User ${socket.userId} joined personal room`);
// Émettre les tâches existantes à la connexion
socket.emit("connected", {
message: "Connexion établie",
userId: socket.userId,
timestamp: new Date(),
});
}
// Événements personnalisés
socket.on("joinTaskRoom", (taskId) => {
if (!taskId || typeof taskId !== "string") {
socket.emit("error", { message: "Invalid task ID" });
return;
}
socket.join(`task_${taskId}`);
console.log(`Socket ${socket.id} joined task room: ${taskId}`);
});
socket.on("leaveTaskRoom", (taskId) => {
if (!taskId || typeof taskId !== "string") {
socket.emit("error", { message: "Invalid task ID" });
return;
}
socket.leave(`task_${taskId}`);
console.log(`Socket ${socket.id} left task room: ${taskId}`);
});
// Gestion des statuts en ligne
socket.on("userOnline", () => {
if (socket.userId) {
socket.broadcast.emit("userStatusChanged", {
userId: socket.userId,
status: "online",
});
}
});
socket.on("error", (error) => {
console.error("Socket error:", error);
});
socket.on("disconnect", () => {
console.log(`Client disconnected: ${socket.id}`);
if (socket.userId) {
socket.broadcast.emit("userStatusChanged", {
userId: socket.userId,
status: "offline",
});
}
});
});
// Register routes APRÈS Socket.IO
await app.register(taskRoutes, { prefix: "/api/v1" });
await app.register(authRoute, { prefix: "/api/v1/auth" });
await app.register(commentRoutes, { prefix: "/api/v1/comment" });
await app.register(roomRoutes, { prefix: "/api/v1/room" });
await app.register(notificationRoutes, { prefix: "/api/v1/notification" });
// Routes pour les notifications en temps réel
app.post("/api/v1/broadcast", async (request, reply) => {
const { event, data, room } = request.body;
if (!event || typeof event !== "string") {
return reply.code(400).send({
success: false,
error: "Event name is required",
});
}
if (room) {
io.to(room).emit(event, data);
} else {
io.emit(event, data);
}
return reply.send({ success: true });
});
// Route pour notification à un utilisateur spécifique
app.post("/api/v1/notify-user", async (request, reply) => {
const { userId, event, data } = request.body;
io.to(`user_${userId}`).emit(event, data);
return reply.send({ success: true });
});
// Start server
const PORT = process.env.PORT || 3000;
await app.listen({ port: PORT, host: "0.0.0.0" });
console.log(`🚀 Server running on http://localhost:${PORT}`);
console.log(`📡 Socket.IO ready for real-time connections`);
} catch (error) {
app.log.error(error);
process.exit(1);
}
}
startServer();