implementation of module and service socket
This commit is contained in:
@@ -1,7 +1,8 @@
|
|||||||
import { Module } from "@nestjs/common";
|
import { Module } from "@nestjs/common";
|
||||||
import { NotificationModule } from "./notifications/notification.mudule";
|
import { NotificationModule } from "./notifications/notification.module";
|
||||||
import { SequelizeModule } from "@nestjs/sequelize";
|
import { SequelizeModule } from "@nestjs/sequelize";
|
||||||
import { ConfigModule } from "@nestjs/config";
|
import { ConfigModule } from "@nestjs/config";
|
||||||
|
import { SocketModule } from "./socket/socket.module";
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
@@ -17,6 +18,7 @@ import { ConfigModule } from "@nestjs/config";
|
|||||||
synchronize: true,
|
synchronize: true,
|
||||||
}),
|
}),
|
||||||
NotificationModule,
|
NotificationModule,
|
||||||
|
SocketModule,
|
||||||
],
|
],
|
||||||
})
|
})
|
||||||
export class AppModule {
|
export class AppModule {
|
||||||
|
|||||||
@@ -1,18 +1,16 @@
|
|||||||
import { Body, Controller, Post } from "@nestjs/common";
|
import { Body, Controller, Post } from "@nestjs/common";
|
||||||
|
import { NotificationDto } from "./notification.dto";
|
||||||
import { NotificationService } from "./notification.service";
|
import { NotificationService } from "./notification.service";
|
||||||
import { SendNotificationDto } from "./validations/sendNotification.dto";
|
|
||||||
import { SocketGateway } from "../socket/socket.gateway";
|
|
||||||
|
|
||||||
@Controller("notifications")
|
@Controller("notifications")
|
||||||
export class NotificationController {
|
export class NotificationController {
|
||||||
constructor(
|
|
||||||
private readonly notificationService: NotificationService,
|
constructor(private notificationService: NotificationService) {
|
||||||
private socketGateway: SocketGateway,
|
|
||||||
) {
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Post("send")
|
@Post("send")
|
||||||
async sendNotification(@Body() notificationData: SendNotificationDto) {
|
async sendNotification(@Body() notificationData: NotificationDto) {
|
||||||
return await this.socketGateway.handleSendNotification(notificationData);
|
return await this.notificationService.sendNotification(notificationData);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
import { IsInt, IsNotEmpty, IsString } from "class-validator";
|
import { IsInt, IsNotEmpty, IsString } from "class-validator";
|
||||||
|
|
||||||
export class SendNotificationDto {
|
export class NotificationDto {
|
||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
@IsInt()
|
@IsInt()
|
||||||
call_id: number;
|
call_id: number;
|
||||||
12
src/notifications/notification.module.ts
Normal file
12
src/notifications/notification.module.ts
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
import { Module } from "@nestjs/common";
|
||||||
|
import { NotificationController } from "./notification.controller";
|
||||||
|
import { NotificationService } from "./notification.service";
|
||||||
|
import { SocketModule } from "../socket/socket.module";
|
||||||
|
|
||||||
|
@Module({
|
||||||
|
imports: [SocketModule],
|
||||||
|
controllers: [NotificationController],
|
||||||
|
providers: [NotificationService],
|
||||||
|
})
|
||||||
|
export class NotificationModule {
|
||||||
|
}
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
import { Module } from "@nestjs/common";
|
|
||||||
import { NotificationController } from "./notification.controller";
|
|
||||||
import { NotificationService } from "./notification.service";
|
|
||||||
import { SocketGateway } from "../socket/socket.gateway";
|
|
||||||
import { SocketService } from "../socket/socket.service";
|
|
||||||
import { SequelizeModule } from "@nestjs/sequelize";
|
|
||||||
import { SocketModel } from "../socket/models/socket.model";
|
|
||||||
|
|
||||||
@Module({
|
|
||||||
imports: [SequelizeModule.forFeature([SocketModel])],
|
|
||||||
controllers: [NotificationController],
|
|
||||||
providers: [NotificationService, SocketGateway, SocketService],
|
|
||||||
})
|
|
||||||
export class NotificationModule {
|
|
||||||
}
|
|
||||||
@@ -1,5 +1,15 @@
|
|||||||
import { Injectable } from "@nestjs/common";
|
import { Injectable } from "@nestjs/common";
|
||||||
|
import { SocketGateway } from "../socket/socket.gateway";
|
||||||
|
import { NotificationInterface } from "./notification.interface";
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class NotificationService {
|
export class NotificationService {
|
||||||
|
constructor(
|
||||||
|
private socketGateway: SocketGateway,
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
|
||||||
|
async sendNotification(notificationData: NotificationInterface) {
|
||||||
|
return await this.socketGateway.handleSendNotification(notificationData);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
import { OnGatewayConnection, SubscribeMessage, WebSocketGateway, WebSocketServer } from "@nestjs/websockets";
|
import { OnGatewayConnection, SubscribeMessage, WebSocketGateway, WebSocketServer } from "@nestjs/websockets";
|
||||||
import { Server, Socket } from "socket.io";
|
import { Server, Socket } from "socket.io";
|
||||||
import { NotificationInterface } from "../notifications/interfaces/notification.interface";
|
import { NotificationInterface } from "../notifications/notification.interface";
|
||||||
import { SocketService } from "./socket.service";
|
import { SocketService } from "./socket.service";
|
||||||
|
|
||||||
@WebSocketGateway()
|
@WebSocketGateway()
|
||||||
@@ -30,32 +30,6 @@ export class SocketGateway implements OnGatewayConnection {
|
|||||||
|
|
||||||
@SubscribeMessage("sendNotificationToClients")
|
@SubscribeMessage("sendNotificationToClients")
|
||||||
handleSendNotification(notificationData: NotificationInterface) {
|
handleSendNotification(notificationData: NotificationInterface) {
|
||||||
return new Promise(async (resolve) => {
|
return this.socketService.sendNotificationToClients(this.server, notificationData);
|
||||||
const connectedClients = this.server.sockets.sockets;
|
|
||||||
|
|
||||||
if (connectedClients.size === 0) resolve({ status: "failed", message: "Clients not found!" });
|
|
||||||
|
|
||||||
let sent: any = false;
|
|
||||||
|
|
||||||
for (const connectedClient of connectedClients) {
|
|
||||||
if (connectedClient[1].handshake.auth.token == notificationData.telephone_id) {
|
|
||||||
sent = await new Promise((resolve) => {
|
|
||||||
connectedClient[1].timeout(2000).emit("answer", JSON.stringify(notificationData), (err: any) => {
|
|
||||||
if (err) {
|
|
||||||
resolve(false);
|
|
||||||
} else {
|
|
||||||
resolve(true);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sent) {
|
|
||||||
resolve({ status: "success", message: "Sent to clients" });
|
|
||||||
} else {
|
|
||||||
resolve({ status: "failed", message: "Not sent to clients" });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
13
src/socket/socket.module.ts
Normal file
13
src/socket/socket.module.ts
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
import { Module } from "@nestjs/common";
|
||||||
|
import { SocketGateway } from "./socket.gateway";
|
||||||
|
import { SocketService } from "./socket.service";
|
||||||
|
import { SequelizeModule } from "@nestjs/sequelize";
|
||||||
|
import { SocketModel } from "./socket.model";
|
||||||
|
|
||||||
|
@Module({
|
||||||
|
imports: [SequelizeModule.forFeature([SocketModel])],
|
||||||
|
providers: [SocketGateway, SocketService],
|
||||||
|
exports: [SocketGateway],
|
||||||
|
})
|
||||||
|
export class SocketModule {
|
||||||
|
}
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
import { Injectable } from "@nestjs/common";
|
import { Injectable } from "@nestjs/common";
|
||||||
import { InjectModel } from "@nestjs/sequelize";
|
import { InjectModel } from "@nestjs/sequelize";
|
||||||
import { SocketModel } from "./models/socket.model";
|
import { SocketModel } from "./socket.model";
|
||||||
|
import { Server } from "socket.io";
|
||||||
|
import { NotificationInterface } from "../notifications/notification.interface";
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class SocketService {
|
export class SocketService {
|
||||||
@@ -10,7 +12,42 @@ export class SocketService {
|
|||||||
) {
|
) {
|
||||||
}
|
}
|
||||||
|
|
||||||
ClientConnection(data) {
|
ClientConnection(data: any) {
|
||||||
this.socketModel.create(data);
|
this.socketModel.create(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sendNotificationToClients(server: Server, notificationData: NotificationInterface) {
|
||||||
|
return new Promise(async (resolveHandlerSend) => {
|
||||||
|
const connectedClients = server.sockets.sockets;
|
||||||
|
|
||||||
|
if (connectedClients.size === 0) {
|
||||||
|
resolveHandlerSend({ status: 404, message: "Failed to find the clients!" });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let sent: any = false;
|
||||||
|
|
||||||
|
for (const connectedClient of connectedClients) {
|
||||||
|
if (connectedClient[1].handshake.auth.token == notificationData.telephone_id) {
|
||||||
|
sent = await new Promise((resolveSend) => {
|
||||||
|
connectedClient[1].timeout(2000).emit("answer", JSON.stringify(notificationData), (err: any) => {
|
||||||
|
if (err) {
|
||||||
|
resolveSend(false);
|
||||||
|
} else {
|
||||||
|
resolveSend(true);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!sent) {
|
||||||
|
resolveHandlerSend({ status: 404, message: "Failed to send to the clients!" });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
resolveHandlerSend({ status: 200, message: "Success in sending to the clients!" });
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user