-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #177 from muttaqin1/Feat-notification-system
Feat:notification-system
- Loading branch information
Showing
7 changed files
with
132 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { IReq } from '@root/interfaces/vendors'; | ||
import { expressAuthentication } from '@root/middlewares/authentications'; | ||
import TaskEmitter from '@root/utils/TaskEmitter'; | ||
import { NextFunction, Response } from 'express'; | ||
|
||
export default [ | ||
async (req: IReq, res: Response, next: NextFunction) => { | ||
try { | ||
await expressAuthentication(req, 'jwt'); | ||
next(); | ||
} catch (err) { | ||
res.json(err.message); | ||
} | ||
}, | ||
(req: IReq, res: Response) => { | ||
TaskEmitter.registerClient(req, res); | ||
res.writeHead(200, { | ||
'Content-Type': 'text/event-stream', | ||
'Cache-Control': 'no-cache', | ||
'Content-Encoding': 'none', | ||
Connection: 'keep-alive', | ||
}); | ||
TaskEmitter.listenIncomingNotification(); | ||
res.on('close', () => { | ||
TaskEmitter.removeConnectedClient(String(req.user._id)); | ||
res.end(); | ||
}); | ||
}, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { IReq } from '@root/interfaces/vendors'; | ||
import { EventEmitter } from 'events'; | ||
import { Response } from 'express'; | ||
import NotificationHandler, { | ||
NotificationInput, | ||
} from './notification/notification'; | ||
import { | ||
INotification, | ||
SendThrough, | ||
} from '@root/models/notifications/notification_model'; | ||
import Logger from '@utils/logger'; | ||
|
||
class TaskEmitter extends EventEmitter { | ||
public connectedClients: Map<string, Response> = new Map(); | ||
constructor() { | ||
super(); | ||
} | ||
|
||
public emitEvent<T>(event: string, data?: T): void { | ||
this.emit(event, data); | ||
} | ||
public registerClient(req: IReq, res: Response): void { | ||
let userId = String(req.user._id); | ||
if (this.connectedClients.has(userId)) { | ||
this.connectedClients.delete(userId); | ||
this.connectedClients.set(userId, res); | ||
} else { | ||
this.connectedClients.set(userId, res); | ||
} | ||
} | ||
public listenIncomingNotification(): void { | ||
this.on('live-notification', (notification: INotification) => { | ||
if (this.connectedClients.has(notification.receiver.toString())) { | ||
const resObj = this.connectedClients.get( | ||
notification.receiver.toString() | ||
); | ||
const jsonNotificationString = JSON.stringify(notification); | ||
resObj.write(`data: ${jsonNotificationString}\n\n`); | ||
} | ||
}); | ||
} | ||
public async emitNotification( | ||
payload: Omit<NotificationInput, 'sendThrough'> | ||
): Promise<void> { | ||
try { | ||
if (payload.sender.toString() === payload.receiver.toString()) | ||
return; | ||
const notificationInstance = | ||
await NotificationHandler.createNotification({ | ||
...payload, | ||
sendThrough: SendThrough.Notification, | ||
}); | ||
this.emitEvent('live-notification', notificationInstance); | ||
} catch (err) { | ||
Logger.error('Failed to send Notification.'); | ||
Logger.error(err); | ||
} | ||
} | ||
public removeConnectedClient(userId: string) { | ||
this.connectedClients.delete(userId); | ||
} | ||
} | ||
|
||
export default new TaskEmitter(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters