-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
81 lines (66 loc) · 2.09 KB
/
index.js
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
const express = require("express");
const cors = require("cors");
const mongoose = require("mongoose");
const authRoutes = require("./routes/auth");
const messageRoutes = require("./routes/messages");
const dotenv = require("dotenv");
const socket = require("socket.io");
dotenv.config();
const app = express();
const DB = process.env.DB;
app.use(cors({
origin: 'https://quick-convo.vercel.app',
credentials: true
}));
app.use(express.json());
app.use("/api/auth", authRoutes);
app.use("/api/messages", messageRoutes);
mongoose.set('strictQuery', true);
// Connect to MongoDB
mongoose.connect(DB, {
useNewUrlParser: true,
useUnifiedTopology: true,
}).then(() => {
console.log("Connected to MongoDB (cloud)");
}).catch((err) => {
console.error("MongoDB connection error:", err.message);
});
const server = app.listen(process.env.PORT, () => {
console.log(`Server started on PORT ${process.env.PORT}`);
});
app.get('/', (req, res) => {
res.send('Hello World! Server is running.');
});
const io = socket(server, {
cors: {
origin: "https://quick-convo.vercel.app",
methods: ["GET", "POST"],
credentials: true
},
});
global.onlineUsers = new Map();
io.on("connection", (socket) => {
console.log("New user connected:", socket.id);
global.chatSocket = socket;
socket.on("add-user", (userId) => {
onlineUsers.set(userId, socket.id);
console.log(`User added: ${userId} with socket ID: ${socket.id}`);
});
socket.on("sendMessage", (data) => {
const sendUserSocket = onlineUsers.get(data.to);
if (sendUserSocket) {
socket.to(sendUserSocket).emit("receiveMessage", data.msg);
console.log(`Message sent to ${data.to}: ${data.msg}`);
}
});
// Handle disconnection
socket.on("disconnect", () => {
console.log("User disconnected:", socket.id);
onlineUsers.forEach((value, key) => {
if (value === socket.id) {
onlineUsers.delete(key);
console.log(`User removed: ${key}`);
}
});
});
});