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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
|
// lib/realtime/RealtimeNotificationService.ts
import notificationManager from './NotificationManager';
interface ConnectedClient {
userId: string;
controller: ReadableStreamDefaultController;
lastHeartbeat: number;
connectionId: string;
userAgent?: string;
ipAddress?: string;
}
class RealtimeNotificationService {
private connectedClients = new Map<string, ConnectedClient[]>();
private heartbeatInterval: NodeJS.Timeout | null = null;
private clientTimeout = 5 * 60 * 1000; // 5분
private heartbeatFrequency = 30 * 1000; // 30초
constructor() {
this.setupEventListeners();
this.startHeartbeat();
this.startConnectionCleanup();
}
private setupEventListeners() {
// PostgreSQL NOTIFY 이벤트 수신 시 클라이언트들에게 브로드캐스트
notificationManager.on('newNotification', (payload: any) => {
console.log('Broadcasting new notification to user:', payload.user_id);
this.broadcastToUser(payload.user_id, {
type: 'new_notification',
data: payload
});
});
notificationManager.on('notificationRead', (payload: any) => {
console.log('Broadcasting notification read to user:', payload.user_id);
this.broadcastToUser(payload.user_id, {
type: 'notification_read',
data: payload
});
});
// NotificationManager 연결 상태 변화 시 클라이언트들에게 알림
notificationManager.on('connected', () => {
this.broadcastToAllUsers({
type: 'system_status',
data: { status: 'connected', message: 'Real-time notifications are now active' }
});
});
notificationManager.on('disconnected', () => {
this.broadcastToAllUsers({
type: 'system_status',
data: { status: 'disconnected', message: 'Real-time notifications temporarily unavailable' }
});
});
}
// 클라이언트 연결 등록
public addClient(
userId: string,
controller: ReadableStreamDefaultController,
metadata?: { userAgent?: string; ipAddress?: string }
): string {
const connectionId = `${userId}_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
if (!this.connectedClients.has(userId)) {
this.connectedClients.set(userId, []);
}
const clients = this.connectedClients.get(userId)!;
// 동일 사용자의 최대 연결 수 제한
const maxConnectionsPerUser = parseInt(process.env.MAX_CONNECTIONS_PER_USER || '5');
if (clients.length >= maxConnectionsPerUser) {
console.warn(`Max connections (${maxConnectionsPerUser}) reached for user ${userId}`);
// 가장 오래된 연결 제거
const oldestClient = clients.shift();
if (oldestClient) {
try {
oldestClient.controller.close();
} catch (error) {
console.warn('Error closing oldest connection:', error);
}
}
}
clients.push({
userId,
controller,
lastHeartbeat: Date.now(),
connectionId,
userAgent: metadata?.userAgent,
ipAddress: metadata?.ipAddress
});
console.log(`Client ${connectionId} connected for user ${userId} (total: ${clients.length})`);
// 연결 확인 메시지 전송
this.sendToClient(controller, {
type: 'connected',
data: {
connectionId,
serverTime: new Date().toISOString(),
dbStatus: notificationManager.getConnectionStatus()
}
});
return connectionId;
}
// 클라이언트 연결 해제
public removeClient(userId: string, controller: ReadableStreamDefaultController): void {
const clients = this.connectedClients.get(userId);
if (!clients) return;
const filteredClients = clients.filter(client => {
if (client.controller === controller) {
console.log(`Client ${client.connectionId} disconnected for user ${userId}`);
return false;
}
return true;
});
if (filteredClients.length === 0) {
this.connectedClients.delete(userId);
console.log(`All clients disconnected for user ${userId}`);
} else {
this.connectedClients.set(userId, filteredClients);
}
}
// 특정 사용자에게 메시지 브로드캐스트
private broadcastToUser(userId: string, message: any): void {
const clients = this.connectedClients.get(userId);
if (!clients || clients.length === 0) {
console.log(`No connected clients for user ${userId}`);
return;
}
const activeClients: ConnectedClient[] = [];
let sentCount = 0;
for (const client of clients) {
if (this.sendToClient(client.controller, message)) {
client.lastHeartbeat = Date.now();
activeClients.push(client);
sentCount++;
}
}
// 활성 클라이언트만 유지
if (activeClients.length === 0) {
this.connectedClients.delete(userId);
} else {
this.connectedClients.set(userId, activeClients);
}
console.log(`Message sent to ${sentCount}/${clients.length} clients for user ${userId}`);
}
// 모든 사용자에게 브로드캐스트 (시스템 메시지용)
private broadcastToAllUsers(message: any): void {
let totalSent = 0;
let totalClients = 0;
for (const [userId, clients] of this.connectedClients.entries()) {
totalClients += clients.length;
const activeClients: ConnectedClient[] = [];
for (const client of clients) {
if (this.sendToClient(client.controller, message)) {
client.lastHeartbeat = Date.now();
activeClients.push(client);
totalSent++;
}
}
if (activeClients.length === 0) {
this.connectedClients.delete(userId);
} else {
this.connectedClients.set(userId, activeClients);
}
}
console.log(`System message sent to ${totalSent}/${totalClients} clients`);
}
// 개별 클라이언트에게 메시지 전송
private sendToClient(controller: ReadableStreamDefaultController, message: any): boolean {
try {
const encoder = new TextEncoder();
const data = `data: ${JSON.stringify(message)}\n\n`;
controller.enqueue(encoder.encode(data));
return true;
} catch (error) {
console.warn('Failed to send message to client:', error);
return false;
}
}
// 하트비트 전송 (연결 유지)
private startHeartbeat(): void {
this.heartbeatInterval = setInterval(() => {
const heartbeatMessage = {
type: 'heartbeat',
data: {
timestamp: new Date().toISOString(),
serverStatus: 'healthy',
dbStatus: notificationManager.getConnectionStatus()
}
};
this.broadcastToAllUsers(heartbeatMessage);
}, this.heartbeatFrequency);
console.log(`Heartbeat started with ${this.heartbeatFrequency}ms interval`);
}
// 비활성 연결 정리
private startConnectionCleanup(): void {
setInterval(() => {
const now = Date.now();
let cleanedConnections = 0;
for (const [userId, clients] of this.connectedClients.entries()) {
const activeClients = clients.filter(client => {
const isActive = (now - client.lastHeartbeat) < this.clientTimeout;
if (!isActive) {
try {
client.controller.close();
} catch (error) {
// 이미 닫힌 연결일 수 있음
}
cleanedConnections++;
}
return isActive;
});
if (activeClients.length === 0) {
this.connectedClients.delete(userId);
} else {
this.connectedClients.set(userId, activeClients);
}
}
if (cleanedConnections > 0) {
console.log(`Cleaned up ${cleanedConnections} inactive connections`);
}
}, 60000); // 1분마다 정리
}
// 연결된 클라이언트 수 조회
public getConnectedClientCount(): number {
let count = 0;
for (const clients of this.connectedClients.values()) {
count += clients.length;
}
return count;
}
// 특정 사용자의 연결된 클라이언트 수 조회
public getUserClientCount(userId: string): number {
return this.connectedClients.get(userId)?.length || 0;
}
// 연결된 사용자 수 조회
public getConnectedUserCount(): number {
return this.connectedClients.size;
}
// 상세 연결 정보 조회 (관리자용)
public getDetailedConnectionInfo() {
const info = {
totalClients: this.getConnectedClientCount(),
totalUsers: this.getConnectedUserCount(),
dbConnectionStatus: notificationManager.getConnectionStatus(),
connections: {} as any
};
for (const [userId, clients] of this.connectedClients.entries()) {
info.connections[userId] = clients.map(client => ({
connectionId: client.connectionId,
lastHeartbeat: new Date(client.lastHeartbeat).toISOString(),
userAgent: client.userAgent,
ipAddress: client.ipAddress,
connectedFor: Date.now() - client.lastHeartbeat
}));
}
return info;
}
// 특정 사용자의 모든 연결 해제 (관리자용)
public disconnectUser(userId: string): number {
const clients = this.connectedClients.get(userId);
if (!clients) return 0;
const count = clients.length;
for (const client of clients) {
try {
this.sendToClient(client.controller, {
type: 'force_disconnect',
data: { reason: 'Administrative action' }
});
client.controller.close();
} catch (error) {
console.warn(`Error disconnecting client ${client.connectionId}:`, error);
}
}
this.connectedClients.delete(userId);
console.log(`Forcibly disconnected ${count} clients for user ${userId}`);
return count;
}
// 서비스 종료
public shutdown(): void {
console.log('Shutting down RealtimeNotificationService...');
if (this.heartbeatInterval) {
clearInterval(this.heartbeatInterval);
this.heartbeatInterval = null;
}
// 모든 연결에게 종료 메시지 전송 후 연결 해제
const shutdownMessage = {
type: 'server_shutdown',
data: { message: 'Server is shutting down. Please reconnect in a moment.' }
};
for (const clients of this.connectedClients.values()) {
for (const client of clients) {
try {
this.sendToClient(client.controller, shutdownMessage);
client.controller.close();
} catch (error) {
// 이미 종료된 연결 무시
}
}
}
this.connectedClients.clear();
console.log('RealtimeNotificationService shutdown complete');
}
}
// 싱글톤 인스턴스
const realtimeNotificationService = new RealtimeNotificationService();
// 프로세스 종료 시 정리
process.on('SIGINT', () => {
realtimeNotificationService.shutdown();
});
process.on('SIGTERM', () => {
realtimeNotificationService.shutdown();
});
export default realtimeNotificationService;
|