Skip to content

Commit

Permalink
Add notification_controller.
Browse files Browse the repository at this point in the history
  • Loading branch information
muttaqin1 committed Jul 27, 2024
1 parent 21596e8 commit fd83558
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 25 deletions.
28 changes: 3 additions & 25 deletions backend-app/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import cookieParser from 'cookie-parser';
// import routesVersioning from 'express-routes-versioning';
// import indexRouter from './routes/index';
import { RegisterRoutes } from './routes';
import TaskEmitter from '@root/utils/TaskEmitter';
import { expressAuthentication } from './middlewares/authentications';
import notification_controller from './controllers/notification_controller';

const app = express();

// use json as default format
Expand Down Expand Up @@ -71,29 +71,7 @@ app.use(handleAPIVersion);
// handle bearer token
app.use(bearerToken());

app.get(
'/streams',
async (req, res, next) => {
await expressAuthentication(req, 'jwt');
next();
},
(req, res) => {
TaskEmitter.registerClient(req, res);
console.log('Client connnected...');
res.writeHead(200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
'Content-Encoding': 'none',
Connection: 'keep-alive',
});
TaskEmitter.listenIncomingNotification();
res.on('close', () => {
console.log('Client disconnected.');
TaskEmitter.removeConnectedClient(String(req.user._id));
res.end();
});
}
);
app.get('/streams', notification_controller);

app.get('/', (_req: Request, res: Response) => {
res.status(200).json({
Expand Down
29 changes: 29 additions & 0 deletions backend-app/controllers/notification_controller.ts
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();
});
},
];

0 comments on commit fd83558

Please sign in to comment.