-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ts
52 lines (40 loc) · 1.13 KB
/
server.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
import { Server } from "socket.io";
process.on("uncaughtException", (err) => {
console.log("UNHANDLED Exception! Shutting down...");
console.log(err);
process.exit(1);
});
import app from "./app";
const port = process.env.PORT || 8000;
const server = app.listen(port, () => {
console.log(`Server is running at http://localhost:${port}`);
});
const io = new Server(server, {
cors: {
origin: "*",
},
});
io.on("connection", (socket) => {
socket.on("join-chats", (chatIds) => {
socket.join(chatIds);
});
socket.on("chat-message", (data) => {
const parsedData = JSON.parse(data);
socket.to(parsedData.chatId).emit("received-message", data);
});
socket.on("is-typing", (currentChatId) => {
socket.to(currentChatId).emit("is-typing");
});
socket.on("stopped-typing", (currentChatId) => {
socket.to(currentChatId).emit("stopped-typing");
});
socket.on("delete-chat", (chatId) => {
socket.to(chatId).emit("delete-chat", chatId);
});
socket.on("leave-chats", (chatIds) => {
socket.leave(chatIds);
});
socket.on("disconnect", () => {
console.log("User disconnected");
});
});