-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
127 lines (95 loc) · 3.11 KB
/
Copy pathserver.js
File metadata and controls
127 lines (95 loc) · 3.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
import app from "./app.js";
import { v4 as uuid } from "uuid";
import { Server } from "socket.io";
import { createServer } from "http";
import {
NEW_MESSAGE,
NEW_MESSAGE_ALEART,
START_TYPING,
STOP_TYPING,
} from "./constants/Events.js";
import { connectDB } from "./config/db.js";
import { getSockets } from "./lib/helper.js";
import messageModel from "./models/message.model.js";
// Cloudinary Import
import { v2 as cloudinary } from "cloudinary";
import cookieParser from "cookie-parser";
import { socketAuthentication } from "./middlewares/Auth.js";
connectDB();
// Connect to cloudinary
cloudinary.config({
cloud_name: process.env.CLOUDINARY_CLOUD_NAME,
api_key: process.env.CLOUDINARY_API_KEY,
api_secret: process.env.CLOUDINARY_API_SECRET,
});
// Users socket ids
export const usersSocketIDs = new Map();
const server = createServer(app);
const io = new Server(server, {
cors: {
origin: process.env.FRONTEND_URL,
// methods: ["GET", "PUT", "POST", "DELETE"],
credentials: true,
},
});
app.set("io", io);
// socket io middleware
io.use((socket, next) => {
cookieParser()(socket.request, socket.request.res, async (err) => {
await socketAuthentication(err, socket, next);
});
});
// 6h 47 min in video
io.on("connection", (socket) => {
console.log("user connected", socket.id);
const user = socket.user;
usersSocketIDs.set(user._id.toString(), socket.id);
socket.on(NEW_MESSAGE, async ({ chatId, members, message }) => {
const messageForRealTime = {
content: message,
_id: uuid(),
sender: {
_id: user._id, //user.id,
name: user.name, //user.name
},
chatId,
createdAt: new Date().toISOString(),
};
const messageForDb = {
content: message,
sender: user._id, //user.id,
chat: chatId,
};
console.log("MEMBERS===================", members);
const membersSocket = getSockets(members);
console.log("Heere All users Socket", membersSocket);
io.to(membersSocket).emit(NEW_MESSAGE, {
chatId,
message: messageForRealTime,
});
io.to(membersSocket).emit(NEW_MESSAGE_ALEART, { chatId });
console.log("New message", messageForRealTime);
try {
await messageModel.create(messageForDb);
} catch (error) {
console.log(error);
}
});
// STRAT TYPING
socket.on(START_TYPING, ({ chatId, members }) => {
const membersSocket = getSockets(members);
socket.to(membersSocket).emit(START_TYPING, { chatId });
});
// STOP TYPING
socket.on(STOP_TYPING, ({ chatId, members }) => {
const membersSocket = getSockets(members);
socket.to(membersSocket).emit(STOP_TYPING, { chatId });
});
socket.on("disconnect", () => {
console.log("user disconnected");
usersSocketIDs.delete(user._id.toString());
});
});
server.listen(process.env.PORT, () =>
console.log(`Server is running on port ${process.env.PORT}`)
);