-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket.ts
151 lines (133 loc) · 4.59 KB
/
socket.ts
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
import { Server } from "socket.io";
import Message from "./models/Message";
import User from "./models/User";
import webpush from "web-push";
if (!process.env.VAPID_PUBLIC_KEY || !process.env.VAPID_PRIVATE_KEY) {
throw new Error("Please define the VAPID_PUBLIC_KEY and VAPID_PRIVATE_KEY environment variables.");
}
webpush.setVapidDetails(
"mailto:" + process.env.EMAIL_USERNAME,
process.env.VAPID_PUBLIC_KEY,
process.env.VAPID_PRIVATE_KEY
);
export enum SocketEvents {
USER_CONNECTED = "user connected",
USER_DISCONNECTED = "user disconnected",
PRIVATE_MESSAGE = "private message",
USER_IDS = "userIds",
CONNECT_ERROR = "connect error",
CONNECTION = "connection",
DISCONNECT = "disconnect",
DISCONNECTING = "disconnecting",
CONNECT = "connect",
MESSAGE_READ = "message read",
USER_UPDATED = "user updated",
CHAT_CREATED = "chat created",
}
export const runSocket = (server: any) => {
const io = new Server(server, {
cors: {
origin: process.env.CLIENT_URL,
},
});
io.use((socket, next) => {
const userId = socket.handshake.auth.userId;
if (!userId) {
return next(new Error("User error"));
}
socket.data.userId = userId;
next();
});
io.on(SocketEvents.CONNECTION, async (socket) => {
socket.join(socket.data.userId);
const chats = socket.handshake.auth.rooms;
if (chats?.length) {
await socket.join(chats);
}
const userIds = await Promise.all(
chats.map(async (chatId: string) => {
let resultUserId = "";
const matchingSockets = await io.in(chatId).fetchSockets();
const anotherUserIds = matchingSockets.filter(
(s: any) => s.data.userId !== socket.data.userId
);
if (anotherUserIds.length) {
resultUserId = anotherUserIds[0].data.userId;
}
return resultUserId;
})
);
socket.emit(SocketEvents.USER_IDS, userIds);
socket.rooms.forEach((room) => {
socket.to(room).emit(SocketEvents.USER_CONNECTED, socket.data.userId);
});
socket.on(SocketEvents.PRIVATE_MESSAGE, async (messageObj) => {
const message = new Message(messageObj);
const savedMessage = await message.save();
socket
.to(messageObj.chatId)
.emit(SocketEvents.PRIVATE_MESSAGE, savedMessage);
socket.emit(SocketEvents.PRIVATE_MESSAGE, savedMessage);
const anotherUser = await User.findById(messageObj.to);
if (anotherUser?.notificationSubscription) {
const notificationPayload = {
notification: {
title: messageObj.senderName,
body: messageObj.text,
icon: "/assets/icons/icon-192x192.png",
vibrate: [100, 50, 100],
data: {
onActionClick: {
default: {
operation: "focusLastFocusedOrOpen",
url: `${process.env.CLIENT_URL}/chats/${messageObj.chatId}`,
}
}
},
},
};
webpush
.sendNotification(
anotherUser.notificationSubscription,
JSON.stringify(notificationPayload)
)
.then(() => console.log("Successfully sent notification"))
.catch((err) => console.log(err));
}
});
// notify users upon disconnection
socket.on(SocketEvents.DISCONNECTING, async () => {
const rooms = new Set(socket.rooms);
io.in(socket.data.userId)
.fetchSockets()
.then((sockets) => {
if (sockets.length === 1) {
rooms.forEach((room) => {
socket
.to(room)
.emit(SocketEvents.USER_DISCONNECTED, socket.data.userId);
});
User.findByIdAndUpdate(socket.data.userId, {
lastSeen: Date.now(),
}).exec();
}
});
});
socket.on(SocketEvents.MESSAGE_READ, ({ chatId, messageId }) => {
Message.findByIdAndUpdate(messageId, { isRead: true }).exec();
socket.to(chatId).emit(SocketEvents.MESSAGE_READ, { messageId, chatId });
});
socket.on(SocketEvents.USER_UPDATED, ({ _id, ...rest }) => {
socket.rooms.forEach((room) => {
socket.to(room).emit(SocketEvents.USER_UPDATED, { _id, ...rest });
});
socket.emit(SocketEvents.USER_UPDATED, { _id, ...rest });
});
socket.on(SocketEvents.CHAT_CREATED, async ({ chatId, anotherUserId }) => {
socket.join(chatId);
socket.in(anotherUserId).socketsJoin(chatId);
socket.to(chatId).emit(SocketEvents.USER_CONNECTED, socket.data.userId);
socket.to(chatId).emit(SocketEvents.USER_CONNECTED, anotherUserId);
});
});
};