feat: Added websocket with lib socket.io

This commit is contained in:
Davidson Gomes 2023-08-02 13:08:30 -03:00
parent ed5e66e430
commit 79864e97d6
22 changed files with 72 additions and 19 deletions

View File

@ -6,6 +6,7 @@
* Added extra files for chatwoot and appsmith * Added extra files for chatwoot and appsmith
* Added Get Last Message and Archive for Chat * Added Get Last Message and Archive for Chat
* Added env var QRCODE_COLOR * Added env var QRCODE_COLOR
* Added websocket with lib socket.io
### Fixed ### Fixed

View File

@ -73,6 +73,7 @@
"qrcode-terminal": "^0.12.0", "qrcode-terminal": "^0.12.0",
"redis": "^4.6.5", "redis": "^4.6.5",
"sharp": "^0.30.7", "sharp": "^0.30.7",
"socket.io": "^4.7.1",
"uuid": "^9.0.0" "uuid": "^9.0.0"
}, },
"devDependencies": { "devDependencies": {

40
src/libs/socket.ts Normal file
View File

@ -0,0 +1,40 @@
import { Server } from 'http';
import { Server as SocketIO } from 'socket.io';
import { configService, Cors } from '../config/env.config';
import { Logger } from '../config/logger.config';
const logger = new Logger('Socket');
let io: SocketIO;
const cors = configService.get<Cors>('CORS').ORIGIN;
export const initIO = (httpServer: Server) => {
logger.verbose('Initializing Socket.io');
io = new SocketIO(httpServer, {
cors: {
origin: cors,
},
});
io.on('connection', (socket) => {
logger.verbose('Client connected');
socket.on('disconnect', () => {
logger.verbose('Client disconnected');
});
});
return io;
};
export const getIO = (): SocketIO => {
logger.verbose('Getting Socket.io');
if (!io) {
logger.error('Socket.io not initialized');
throw new Error('Socket.io not initialized');
}
return io;
};

View File

@ -9,6 +9,7 @@ import { configService, Cors, HttpServer } from './config/env.config';
import { onUnexpectedError } from './config/error.config'; import { onUnexpectedError } from './config/error.config';
import { Logger } from './config/logger.config'; import { Logger } from './config/logger.config';
import { ROOT_DIR } from './config/path.config'; import { ROOT_DIR } from './config/path.config';
import { initIO } from './libs/socket';
import { ServerUP } from './utils/server-up'; import { ServerUP } from './utils/server-up';
import { HttpStatus, router } from './whatsapp/routers/index.router'; import { HttpStatus, router } from './whatsapp/routers/index.router';
import { waMonitor } from './whatsapp/whatsapp.module'; import { waMonitor } from './whatsapp/whatsapp.module';
@ -83,6 +84,8 @@ function bootstrap() {
initWA(); initWA();
initIO(server);
onUnexpectedError(); onUnexpectedError();
} }

View File

@ -9,7 +9,7 @@ import {
import { configService, Database } from '../config/env.config'; import { configService, Database } from '../config/env.config';
import { Logger } from '../config/logger.config'; import { Logger } from '../config/logger.config';
import { dbserver } from '../db/db.connect'; import { dbserver } from '../libs/db.connect';
export async function useMultiFileAuthStateDb( export async function useMultiFileAuthStateDb(
coll: string, coll: string,

View File

@ -7,7 +7,7 @@ import {
} from '@whiskeysockets/baileys'; } from '@whiskeysockets/baileys';
import { Logger } from '../config/logger.config'; import { Logger } from '../config/logger.config';
import { RedisCache } from '../db/redis.client'; import { RedisCache } from '../libs/redis.client';
export async function useMultiFileAuthStateRedisDb(cache: RedisCache): Promise<{ export async function useMultiFileAuthStateRedisDb(cache: RedisCache): Promise<{
state: AuthenticationState; state: AuthenticationState;

View File

@ -4,8 +4,8 @@ import EventEmitter2 from 'eventemitter2';
import { ConfigService, HttpServer } from '../../config/env.config'; import { ConfigService, HttpServer } from '../../config/env.config';
import { Logger } from '../../config/logger.config'; import { Logger } from '../../config/logger.config';
import { RedisCache } from '../../db/redis.client';
import { BadRequestException, InternalServerErrorException } from '../../exceptions'; import { BadRequestException, InternalServerErrorException } from '../../exceptions';
import { RedisCache } from '../../libs/redis.client';
import { InstanceDto } from '../dto/instance.dto'; import { InstanceDto } from '../dto/instance.dto';
import { RepositoryBroker } from '../repository/repository.manager'; import { RepositoryBroker } from '../repository/repository.manager';
import { AuthService, OldToken } from '../services/auth.service'; import { AuthService, OldToken } from '../services/auth.service';

View File

@ -4,8 +4,8 @@ import { join } from 'path';
import { configService, Database, Redis } from '../../config/env.config'; import { configService, Database, Redis } from '../../config/env.config';
import { INSTANCE_DIR } from '../../config/path.config'; import { INSTANCE_DIR } from '../../config/path.config';
import { dbserver } from '../../db/db.connect';
import { BadRequestException, ForbiddenException, NotFoundException } from '../../exceptions'; import { BadRequestException, ForbiddenException, NotFoundException } from '../../exceptions';
import { dbserver } from '../../libs/db.connect';
import { InstanceDto } from '../dto/instance.dto'; import { InstanceDto } from '../dto/instance.dto';
import { cache, waMonitor } from '../whatsapp.module'; import { cache, waMonitor } from '../whatsapp.module';

View File

@ -1,6 +1,6 @@
import { Schema } from 'mongoose'; import { Schema } from 'mongoose';
import { dbserver } from '../../db/db.connect'; import { dbserver } from '../../libs/db.connect';
export class AuthRaw { export class AuthRaw {
_id?: string; _id?: string;

View File

@ -1,6 +1,6 @@
import { Schema } from 'mongoose'; import { Schema } from 'mongoose';
import { dbserver } from '../../db/db.connect'; import { dbserver } from '../../libs/db.connect';
export class ChatRaw { export class ChatRaw {
_id?: string; _id?: string;

View File

@ -1,6 +1,6 @@
import { Schema } from 'mongoose'; import { Schema } from 'mongoose';
import { dbserver } from '../../db/db.connect'; import { dbserver } from '../../libs/db.connect';
export class ChatwootRaw { export class ChatwootRaw {
_id?: string; _id?: string;

View File

@ -1,6 +1,6 @@
import { Schema } from 'mongoose'; import { Schema } from 'mongoose';
import { dbserver } from '../../db/db.connect'; import { dbserver } from '../../libs/db.connect';
export class ContactRaw { export class ContactRaw {
_id?: string; _id?: string;

View File

@ -1,6 +1,6 @@
import { Schema } from 'mongoose'; import { Schema } from 'mongoose';
import { dbserver } from '../../db/db.connect'; import { dbserver } from '../../libs/db.connect';
import { wa } from '../types/wa.types'; import { wa } from '../types/wa.types';
class Key { class Key {

View File

@ -1,6 +1,6 @@
import { Schema } from 'mongoose'; import { Schema } from 'mongoose';
import { dbserver } from '../../db/db.connect'; import { dbserver } from '../../libs/db.connect';
export class SettingsRaw { export class SettingsRaw {
_id?: string; _id?: string;

View File

@ -1,6 +1,6 @@
import { Schema } from 'mongoose'; import { Schema } from 'mongoose';
import { dbserver } from '../../db/db.connect'; import { dbserver } from '../../libs/db.connect';
export class WebhookRaw { export class WebhookRaw {
_id?: string; _id?: string;

View File

@ -2,7 +2,7 @@ import { RequestHandler, Router } from 'express';
import { Auth, ConfigService, Database } from '../../config/env.config'; import { Auth, ConfigService, Database } from '../../config/env.config';
import { Logger } from '../../config/logger.config'; import { Logger } from '../../config/logger.config';
import { dbserver } from '../../db/db.connect'; import { dbserver } from '../../libs/db.connect';
import { instanceNameSchema, oldTokenSchema } from '../../validate/validate.schema'; import { instanceNameSchema, oldTokenSchema } from '../../validate/validate.schema';
import { RouterBroker } from '../abstract/abstract.router'; import { RouterBroker } from '../abstract/abstract.router';
import { InstanceDto } from '../dto/instance.dto'; import { InstanceDto } from '../dto/instance.dto';

View File

@ -7,9 +7,9 @@ import { join } from 'path';
import { Auth, ConfigService, Database, DelInstance, HttpServer, Redis } from '../../config/env.config'; import { Auth, ConfigService, Database, DelInstance, HttpServer, Redis } from '../../config/env.config';
import { Logger } from '../../config/logger.config'; import { Logger } from '../../config/logger.config';
import { INSTANCE_DIR, STORE_DIR } from '../../config/path.config'; import { INSTANCE_DIR, STORE_DIR } from '../../config/path.config';
import { dbserver } from '../../db/db.connect';
import { RedisCache } from '../../db/redis.client';
import { NotFoundException } from '../../exceptions'; import { NotFoundException } from '../../exceptions';
import { dbserver } from '../../libs/db.connect';
import { RedisCache } from '../../libs/redis.client';
import { import {
AuthModel, AuthModel,
ChatwootModel, ChatwootModel,

View File

@ -63,9 +63,10 @@ import {
} from '../../config/env.config'; } from '../../config/env.config';
import { Logger } from '../../config/logger.config'; import { Logger } from '../../config/logger.config';
import { INSTANCE_DIR, ROOT_DIR } from '../../config/path.config'; import { INSTANCE_DIR, ROOT_DIR } from '../../config/path.config';
import { dbserver } from '../../db/db.connect';
import { RedisCache } from '../../db/redis.client';
import { BadRequestException, InternalServerErrorException, NotFoundException } from '../../exceptions'; import { BadRequestException, InternalServerErrorException, NotFoundException } from '../../exceptions';
import { dbserver } from '../../libs/db.connect';
import { RedisCache } from '../../libs/redis.client';
import { getIO } from '../../libs/socket';
import { useMultiFileAuthStateDb } from '../../utils/use-multi-file-auth-state-db'; import { useMultiFileAuthStateDb } from '../../utils/use-multi-file-auth-state-db';
import { useMultiFileAuthStateRedisDb } from '../../utils/use-multi-file-auth-state-redis-db'; import { useMultiFileAuthStateRedisDb } from '../../utils/use-multi-file-auth-state-redis-db';
import { import {
@ -416,6 +417,7 @@ export class WAStartupService {
const serverUrl = this.configService.get<HttpServer>('SERVER').URL; const serverUrl = this.configService.get<HttpServer>('SERVER').URL;
const we = event.replace(/[.-]/gm, '_').toUpperCase(); const we = event.replace(/[.-]/gm, '_').toUpperCase();
const transformedWe = we.replace(/_/gm, '-').toLowerCase(); const transformedWe = we.replace(/_/gm, '-').toLowerCase();
const io = getIO();
const expose = this.configService.get<Auth>('AUTHENTICATION').EXPOSE_IN_FETCH_INSTANCES; const expose = this.configService.get<Auth>('AUTHENTICATION').EXPOSE_IN_FETCH_INSTANCES;
const tokenStore = await this.repository.auth.find(this.instanceName); const tokenStore = await this.repository.auth.find(this.instanceName);
@ -553,6 +555,12 @@ export class WAStartupService {
} }
} }
} }
io.emit(event, {
event,
instance: this.instance.name,
data,
});
} }
private async connectionUpdate({ qr, connection, lastDisconnect }: Partial<ConnectionState>) { private async connectionUpdate({ qr, connection, lastDisconnect }: Partial<ConnectionState>) {

View File

@ -1,8 +1,8 @@
import { configService } from '../config/env.config'; import { configService } from '../config/env.config';
import { eventEmitter } from '../config/event.config'; import { eventEmitter } from '../config/event.config';
import { Logger } from '../config/logger.config'; import { Logger } from '../config/logger.config';
import { dbserver } from '../db/db.connect'; import { dbserver } from '../libs/db.connect';
import { RedisCache } from '../db/redis.client'; import { RedisCache } from '../libs/redis.client';
import { ChatController } from './controllers/chat.controller'; import { ChatController } from './controllers/chat.controller';
import { ChatwootController } from './controllers/chatwoot.controller'; import { ChatwootController } from './controllers/chatwoot.controller';
import { GroupController } from './controllers/group.controller'; import { GroupController } from './controllers/group.controller';

View File

@ -11,7 +11,7 @@
<body> <body>
<iframe src="https://app.smith.dgcode.com.br/app/evolutionapi/page1-64c53a7a7ea84639bf879f23?embed=true" frameborder="0" style="width: 100%; height: 100vh;"></iframe> <iframe src="https://app.smith.dgcode.com.br/app/evolutionapi-public/home-64ca60783615e270291978b4?embed=true" frameborder="0" style="width: 100%; height: 100vh;"></iframe>
</body> </body>