-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
182 lines (133 loc) · 5.07 KB
/
index.js
File metadata and controls
182 lines (133 loc) · 5.07 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
require('dotenv').config();
const express = require('express');
const path = require('path');
const cors = require('cors')
const socketIo = require('socket.io');
const moment = require('moment-timezone');
const session = require('express-session')
const flash = require('express-flash')
const passport = require('./src/passport/passport');
const dbConfig = require('./src/config/db.config')
const userRoutes = require('./src/routes/userRoutes');
const chatRoutes = require('./src/routes/chatRoutes');
const Chat = require('./src/models/chat.model')
// Initialize Express app
const app = express();
//***************** Session config ******************//
app.use(session({
secret: process.env.SECRET_KEY,
resave: false,
saveUninitialized: false,
cookie: { maxAge: 1000 * 60 * 60 * 24 } // 24 hour
}))
//***************** Passport setup ******************//
app.use(passport.initialize());
app.use(passport.session());
//***************** Express Flash ******************//
app.use(flash())
//*********** Enable CORS for all routes **************//
app.use(cors());
// ******** serve the static file from the 'public' directory *********//
app.use(express.static(path.join(__dirname, 'public')))
app.use(express.json()) // Parse JSON bodies for incoming requests
app.use(express.urlencoded({ extended: false })); // Parse URL-encoded bodies
// ********* Set Template Engine *********//
app.set("view engine", "ejs")
app.set('views', path.join(__dirname, 'views'))
// ********* Database Connection ************//
dbConfig.connectMongoDB();
app.get('/', (req, res) => {
res.redirect('/register');
});
// ******** Route Setup ***********//
app.use('/', userRoutes);
app.use('/private-chat', chatRoutes);
//***** 404 Error Handling *******/
app.use((req, res, next) => {
res.status(404).render('errors/error', { title: 'Page Not Found' });
});
// *********** Port Start *************//
const PORT = process.env.PORT || 3000;
// ************* Server and Socket Setup ***************//
const server = app.listen(PORT, (err) => {
if (err) {
console.error('❌ Failed to start server:', err.message);
}
else {
console.log(`🚀 Server running on http://localhost:${PORT}`);
}
})
const io = socketIo(server);
// Store active users
const activeUsers = {};
// Socket.IO for real-time chat
io.on('connection', (socket) => {
// console.log(`🔗 User connected: ${socket.id}`);
socket.on('userData', async({ username }) => {
// console.log("Username",username)
// Store user info in the activeUsers
activeUsers[socket.id] = username;
// Store user info in the socket object
socket.username = username;
// Retrieve last 50 messages
const messages = await Chat.find().sort({ date: -1 }).limit(50);
// console.log(messages)
socket.emit('previousMessages', messages.reverse());
// console.log("Active user",activeUsers)
// Notify other users about the new connection
socket.broadcast.emit("notification", { message: `${username} has joined the chat` })
// Update active users list
io.emit('userList', activeUsers);
})
// Send a chat message
socket.on('chatMessage', async (data) => {
const { user, message } = data;
// Validate username structure
if (!user || !user.name || !user.image) {
console.error('❌ Invalid chat message data:', { user, message });
return;
}
const chatData = {
user: {
name: user.name,
image: user.image,
},
message: message,
date: moment().tz("Asia/Kolkata").format('MMMM Do YYYY, h:mm a'),
};
// Broadcast the message to all clients
io.emit('receiveMessage', chatData);
// Save the message to the database
try {
const newMessage = new Chat({
user: { name: user.name.trim(), image: user.image },
message,
date: chatData.date,
});
await newMessage.save();
// console.log('✅ Message saved:', newMessage);
} catch (err) {
console.error('❌ Error saving message:', err.message);
}
})
// Typing Notification
socket.on('typing', (username) => {
// console.log(username)
socket.broadcast.emit('showTyping', username);
});
// Handle built-in 'disconnect' event
socket.on('disconnect', () => {
const username = socket.username;
if (username) {
// console.log(`🔴 ${username} has left the chat`);
// Remove user from the active users list
delete activeUsers[socket.id];
// Updated active users list
io.emit('userList', activeUsers);
// Notify others about the user leaving
io.emit('notification', { message: `${username} has left the chat` });
} else {
// console.log('⚠️ A user disconnected without a username');
}
})
})