-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
389 lines (320 loc) Β· 10.7 KB
/
server.js
File metadata and controls
389 lines (320 loc) Β· 10.7 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
const express = require("express")
const mongoose = require("mongoose")
const cors = require("cors")
const dotenv = require("dotenv")
const http = require("http")
const socketIo = require("socket.io")
const path = require("path")
const jwt = require("jsonwebtoken")
// Load environment variables
dotenv.config()
// Import routes
const authRoutes = require("./routes/authRoutes")
const userRoutes = require("./routes/userRoutes")
const bookingRoutes = require("./routes/bookingRoutes")
const developerSlotRoutes = require("./routes/developerSlotRoutes")
const notificationRoutes = require("./routes/notificationRoutes")
const postRoutes = require("./routes/postRoutes")
const connectionRoutes = require("./routes/connectionRoutes")
const chatRoutes = require("./routes/chatRoutes")
const User = require("./models/User")
const Message = require("./models/Message")
const { emitToUser, joinRoom, leaveRoom } = require("./utils/socketUtils")
const app = express()
const server = http.createServer(app)
const corsOptions = {
origin: process.env.CLIENT_URL || "https://dev-connect1.netlify.app",
credentials: true,
methods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
allowedHeaders: ["Content-Type", "Authorization"],
}
app.use(cors(corsOptions))
// Socket.IO configuration
const io = socketIo(server, {
cors: corsOptions,
transports: ["websocket", "polling"],
})
app.set("io", io)
// Middleware
app.use(express.json({ limit: "10mb" }))
app.use(express.urlencoded({ extended: true, limit: "10mb" }))
// Connect to MongoDB
mongoose
.connect(process.env.MONGODB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => {
console.log("Connected to MongoDB")
})
.catch((error) => {
console.error("MongoDB connection error:", error)
process.exit(1)
})
// Store online users
const onlineUsers = new Map()
// Socket.IO authentication middleware
io.use(async (socket, next) => {
try {
const token = socket.handshake.auth.token
if (!token) {
return next(new Error("Authentication error: No token provided"))
}
const decoded = jwt.verify(token, process.env.JWT_SECRET)
const user = await User.findById(decoded.userId).select("-password")
if (!user) {
return next(new Error("Authentication error: User not found"))
}
socket.userId = user._id.toString()
socket.user = user
next()
} catch (error) {
console.error("Socket authentication error:", error)
next(new Error("Authentication error"))
}
})
// Socket.IO connection handling
io.on("connection", (socket) => {
console.log(`User ${socket.user.name} connected with socket ID: ${socket.id}`)
// Add user to online users
onlineUsers.set(socket.userId, {
socketId: socket.id,
user: socket.user,
lastSeen: new Date(),
})
// Join user's personal room
socket.join(`user_${socket.userId}`)
// Emit online users to all clients
io.emit("online_users", { users: Array.from(onlineUsers.keys()) })
// Notify others that user is online
socket.broadcast.emit("user_online", { userId: socket.userId })
// Handle joining user room
socket.on("join_user_room", (userId) => {
socket.join(`user_${userId}`)
console.log(`User ${socket.user.name} joined room: user_${userId}`)
})
// Handle sending messages
socket.on("send_message", (data) => {
const { receiverId, message } = data
// Emit to receiver
io.to(`user_${receiverId}`).emit("receive_message", message)
// Emit delivery confirmation to sender
socket.emit("message_delivered", { messageId: message._id })
console.log(`Message sent from ${socket.userId} to ${receiverId}`)
})
// Handle typing indicators
socket.on("typing", (data) => {
const { receiverId } = data
io.to(`user_${receiverId}`).emit("user_typing", {
userId: socket.userId,
userName: socket.user.name,
})
})
socket.on("stop_typing", (data) => {
const { receiverId } = data
io.to(`user_${receiverId}`).emit("user_stopped_typing", {
userId: socket.userId,
})
})
// Handle message read receipts
socket.on("mark_messages_read", (data) => {
const { senderId, readBy } = data
io.to(`user_${senderId}`).emit("messages_read", {
readBy,
timestamp: new Date(),
})
})
// Handle booking notifications
socket.on("booking_notification", (data) => {
const { userId, message, type, bookingId } = data
io.to(`user_${userId}`).emit("booking_notification", {
message,
type,
bookingId,
timestamp: new Date(),
})
})
// Handle connection request notifications
socket.on("connection_request", (data) => {
const { receiverId, senderName, senderId } = data
io.to(`user_${receiverId}`).emit("friend_request_notification", {
message: `${senderName} sent you a connection request`,
senderId,
timestamp: new Date(),
})
})
// Handle post interactions
socket.on("post_interaction", (data) => {
const { userId, message, type, postId } = data
io.to(`user_${userId}`).emit("interaction_notification", {
message,
type,
postId,
timestamp: new Date(),
})
})
// Handle WebRTC call events
socket.on("call_user", (data) => {
const { receiverId, offer, callType } = data
console.log(`π Call initiated: ${socket.user.name} -> ${receiverId} (${callType})`)
io.to(`user_${receiverId}`).emit("incoming_call", {
callerId: socket.userId,
callerName: socket.user.name,
callerAvatar: socket.user.avatar,
offer,
callType,
})
})
socket.on("answer_call", (data) => {
const { callerId, answer } = data
console.log(`π Call answered: ${socket.userId} -> ${callerId}`)
io.to(`user_${callerId}`).emit("call_answered", {
answer,
answeredBy: socket.userId,
})
})
socket.on("reject_call", (data) => {
const { callerId } = data
console.log(`π Call rejected: ${socket.userId} from ${callerId}`)
io.to(`user_${callerId}`).emit("call_rejected", {
rejectedBy: socket.userId,
})
})
socket.on("end_call", (data) => {
const { receiverId } = data
console.log(`π Call ended: ${socket.userId} with ${receiverId}`)
io.to(`user_${receiverId}`).emit("call_ended", {
endedBy: socket.userId,
})
})
// Handle ICE candidates for WebRTC
socket.on("ice_candidate", (data) => {
const { receiverId, candidate } = data
console.log(`π§ ICE candidate: ${socket.userId} -> ${receiverId}`)
io.to(`user_${receiverId}`).emit("ice_candidate", {
candidate,
from: socket.userId,
})
})
// Handle disconnect
socket.on("disconnect", (reason) => {
console.log(`User ${socket.user.name} disconnected: ${reason}`)
// Remove user from online users
onlineUsers.delete(socket.userId)
// Update user's last seen
User.findByIdAndUpdate(socket.userId, { lastSeen: new Date() }).catch((error) =>
console.error("Error updating last seen:", error),
)
// Emit updated online users list
io.emit("online_users", { users: Array.from(onlineUsers.keys()) })
// Notify others that user is offline
socket.broadcast.emit("user_offline", { userId: socket.userId })
})
// Handle errors
socket.on("error", (error) => {
console.error(`Socket error for user ${socket.user.name}:`, error)
})
})
// Routes
app.use("/api/auth", authRoutes)
app.use("/api/users", userRoutes)
app.use("/api/bookings", bookingRoutes)
app.use("/api/developer-slots", developerSlotRoutes)
app.use("/api/notifications", notificationRoutes)
app.use("/api/posts", postRoutes)
app.use("/api/connections", connectionRoutes)
app.use("/api/messages", chatRoutes)
// Enhanced health check endpoint
app.get("/api/health", (req, res) => {
const uptime = process.uptime()
const memoryUsage = process.memoryUsage()
res.json({
success: true,
message: "Server is running",
timestamp: new Date().toISOString(),
uptime: `${Math.floor(uptime / 60)} minutes`,
connectedUsers: onlineUsers.size,
memoryUsage: {
rss: `${Math.round(memoryUsage.rss / 1024 / 1024)} MB`,
heapUsed: `${Math.round(memoryUsage.heapUsed / 1024 / 1024)} MB`,
heapTotal: `${Math.round(memoryUsage.heapTotal / 1024 / 1024)} MB`,
},
environment: process.env.NODE_ENV || "development",
})
})
// Get online users endpoint with enhanced info
app.get("/api/online-users", (req, res) => {
const onlineUsersArray = Array.from(onlineUsers.values()).map((user) => ({
userId: user.user._id,
name: user.user.name,
avatar: user.user.avatar,
role: user.user.role,
lastSeen: user.lastSeen,
connectedAt: user.connectedAt,
}))
res.json({
success: true,
onlineUsers: onlineUsersArray,
count: onlineUsersArray.length,
timestamp: new Date().toISOString(),
})
})
// Enhanced error handling middleware
app.use((err, req, res, next) => {
console.error("β Server Error:", err.stack)
// Don't leak error details in production
const isDevelopment = process.env.NODE_ENV === "development"
res.status(err.status || 500).json({
success: false,
message: err.message || "Something went wrong!",
error: isDevelopment ? err.message : "Internal server error",
timestamp: new Date().toISOString(),
})
})
// 404 handler
app.use("*", (req, res) => {
res.status(404).json({
success: false,
message: `Route ${req.originalUrl} not found`,
timestamp: new Date().toISOString(),
})
})
const PORT = process.env.PORT || 5000
server.listen(PORT, () => {
console.log(`π Server running on port ${PORT}`)
console.log(`π Environment: ${process.env.NODE_ENV || "development"}`)
console.log(`π Client URL: ${process.env.CLIENT_URL || "https://dev-connect1.netlify.app"}`)
console.log(`π Health check: http://192.168.0.106:${PORT}/api/health`)
})
// Graceful shutdown
const gracefulShutdown = (signal) => {
console.log(`\nπ Received ${signal}. Shutting down gracefully...`)
server.close(() => {
console.log("β
HTTP server closed")
mongoose.connection.close(false, () => {
console.log("β
MongoDB connection closed")
process.exit(0)
})
})
// Force close after 10 seconds
setTimeout(() => {
console.error("β Could not close connections in time, forcefully shutting down")
process.exit(1)
}, 10000)
}
process.on("SIGTERM", () => gracefulShutdown("SIGTERM"))
process.on("SIGINT", () => gracefulShutdown("SIGINT"))
process.on("unhandledRejection", (err, promise) => {
console.error("β Unhandled Promise Rejection:", err.message)
console.error("Promise:", promise)
server.close(() => {
process.exit(1)
})
})
process.on("uncaughtException", (err) => {
console.error("β Uncaught Exception:", err.message)
console.error("Stack:", err.stack)
server.close(() => {
process.exit(1)
})
})