-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
140 lines (113 loc) · 4.01 KB
/
server.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
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
const express = require("express");
const app = express();
// OLD VERSION taught in the course.
// const server = require("http").Server(app);
// const io = require("socket.io")(server);
// LATEST VERSION Socket io @4.4.1
const { createServer } = require("http");
const { Server } = require("socket.io");
const httpServer = createServer(app);
const io = new Server(httpServer, {
/* options */
});
const next = require("next");
const dev = process.env.NODE_ENV !== "production";
const nextApp = next({ dev });
const handle = nextApp.getRequestHandler();
require("dotenv").config({ path: "./config.env" });
const connectDb = require("./utilsServer/connectDb");
connectDb();
app.use(express.json());
const PORT = process.env.PORT || 3000;
const {
addUser,
removeUser,
findConnectedUser
} = require("./utilsServer/roomActions");
const {
loadMessages,
sendMsg,
setMsgToUnread,
deleteMsg
} = require("./utilsServer/messageActions");
const { likeOrUnlikePost } = require("./utilsServer/likeOrUnlikePost");
io.on("connection", socket => {
socket.on("join", async ({ userId }) => {
const users = await addUser(userId, socket.id);
console.log(users);
setInterval(() => {
socket.emit("connectedUsers", {
users: users.filter(user => user.userId !== userId)
});
}, 10000);
});
socket.on("likePost", async ({ postId, userId, like }) => {
const { success, name, profilePicUrl, username, postByUserId, error } =
await likeOrUnlikePost(postId, userId, like);
if (success) {
socket.emit("postLiked");
if (postByUserId !== userId) {
const receiverSocket = findConnectedUser(postByUserId);
if (receiverSocket && like) {
// WHEN YOU WANT TO SEND DATA TO ONE PARTICULAR CLIENT
io.to(receiverSocket.socketId).emit("newNotificationReceived", {
name,
profilePicUrl,
username,
postId
});
}
}
}
});
socket.on("loadMessages", async ({ userId, messagesWith }) => {
const { chat, error } = await loadMessages(userId, messagesWith);
!error ? socket.emit("messagesLoaded", { chat }) : socket.emit("noChatFound");
});
socket.on("sendNewMsg", async ({ userId, msgSendToUserId, msg }) => {
const { newMsg, error } = await sendMsg(userId, msgSendToUserId, msg);
const receiverSocket = findConnectedUser(msgSendToUserId);
if (receiverSocket) {
// WHEN YOU WANT TO SEND MESSAGE TO A PARTICULAR SOCKET
io.to(receiverSocket.socketId).emit("newMsgReceived", { newMsg });
}
//
else {
await setMsgToUnread(msgSendToUserId);
}
!error && socket.emit("msgSent", { newMsg });
});
socket.on("deleteMsg", async ({ userId, messagesWith, messageId }) => {
const { success } = await deleteMsg(userId, messagesWith, messageId);
if (success) socket.emit("msgDeleted");
});
socket.on("sendMsgFromNotification", async ({ userId, msgSendToUserId, msg }) => {
const { newMsg, error } = await sendMsg(userId, msgSendToUserId, msg);
const receiverSocket = findConnectedUser(msgSendToUserId);
if (receiverSocket) {
// WHEN YOU WANT TO SEND MESSAGE TO A PARTICULAR SOCKET
io.to(receiverSocket.socketId).emit("newMsgReceived", { newMsg });
}
//
else {
await setMsgToUnread(msgSendToUserId);
}
!error && socket.emit("msgSentFromNotification");
});
socket.on("disconnect", () => removeUser(socket.id));
});
nextApp.prepare().then(() => {
app.use("/api/signup", require("./api/signup"));
app.use("/api/auth", require("./api/auth"));
app.use("/api/search", require("./api/search"));
app.use("/api/posts", require("./api/posts"));
app.use("/api/profile", require("./api/profile"));
app.use("/api/notifications", require("./api/notifications"));
app.use("/api/chats", require("./api/chats"));
app.use("/api/reset", require("./api/reset"));
app.all("*", (req, res) => handle(req, res));
httpServer.listen(PORT, err => {
if (err) throw err;
console.log("Express server running");
});
});