-
Notifications
You must be signed in to change notification settings - Fork 0
/
SocketManager.ts
122 lines (100 loc) · 3 KB
/
SocketManager.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
import { Server } from 'socket.io';
import http from 'http';
import jwt from 'jsonwebtoken';
import config from './config';
import logger from './logger';
import { NotificationType } from '../types';
/**
* Manages the socket connections and events for the server.
*/
class SocketManager {
private static instance: SocketManager;
private io: Server;
private users: Map<string, string>;
private constructor(server: http.Server) {
this.io = new Server(server, {
cors: {
origin: '*',
},
});
this.users = new Map();
this.io.use((socket, next) => {
const { accessToken, userId } = socket.handshake.auth;
let decodedToken;
if (accessToken) {
try {
decodedToken = jwt.verify(
accessToken,
config.JWT_SECRET || '',
) as jwt.JwtPayload;
} catch (error) {
const errorMessage = logger.getErrorMessage(error);
logger.error(errorMessage);
}
}
if (
!accessToken
|| !userId
|| !decodedToken
|| !decodedToken.id
|| decodedToken.id !== userId
|| !decodedToken.username
) {
return next(new Error('invalid token'));
}
return next();
});
this.io.on('connection', (socket) => {
const { userId } = socket.handshake.auth;
const socketId = socket.id;
this.users.set(userId, socketId);
logger.info({
message: 'user connected',
userId,
socketId,
});
socket.on('disconnect', () => {
this.users.delete(userId);
logger.info({
message: 'user disconnected',
userId,
socketId,
});
});
});
logger.info('SocketManager initialized');
}
/**
* Returns the singleton instance of the SocketManager class.
* If the instance does not exist, it creates a new instance using the provided http.Server.
* @param server - The http.Server instance to be used for initializing the SocketManager.
* @returns The singleton instance of the SocketManager class.
* @throws Error if the SocketManager instance is not initialized and no http.Server is provided.
*/
public static getInstance(server?: http.Server): SocketManager {
if (!SocketManager.instance) {
if (!server) {
throw new Error('SocketManager requires an http.Server to be initialized');
}
SocketManager.instance = new SocketManager(server);
}
return SocketManager.instance;
}
/**
* Emits a notification to a specific user.
* @param userId - The ID of the user to send the notification to.
* @param notification - The notification to send.
*/
emitNotification(userId: string, notification: NotificationType): void {
const socketId = this.users.get(userId);
if (socketId) {
this.io.to(socketId).emit('notification', notification);
logger.info({
message: 'notification sent',
userId,
notification,
});
}
}
}
export default SocketManager;