@@ -6,16 +6,40 @@ import { NetworkGateway } from 'src/crons/websocket/network.gateway';
66import { Lock } from "@multiversx/sdk-nestjs-common" ;
77import { PoolGateway } from 'src/crons/websocket/pool.gateway' ;
88import { EventsGateway } from 'src/crons/websocket/events.gateway' ;
9+ import { WebSocketGateway , WebSocketServer } from '@nestjs/websockets' ;
10+ import { EventEmitter2 } from '@nestjs/event-emitter' ;
11+ import { MetricsEvents } from 'src/utils/metrics-events.constants' ;
12+ import { Server } from 'socket.io' ;
913@Injectable ( )
14+ @WebSocketGateway ( { cors : { origin : '*' } , path : '/ws/subscription' } )
1015export class WebsocketCronService {
16+ @WebSocketServer ( )
17+ server ! : Server ;
18+
1119 constructor (
1220 private readonly transactionsGateway : TransactionsGateway ,
1321 private readonly blocksGateway : BlocksGateway ,
1422 private readonly networkGateway : NetworkGateway ,
1523 private readonly poolGateway : PoolGateway ,
1624 private readonly eventsGateway : EventsGateway ,
25+ private readonly eventEmitter : EventEmitter2 ,
1726 ) { }
1827
28+ @Cron ( '*/6 * * * * *' )
29+ async handleWebsocketMetrics ( ) {
30+ const connectedClients = this . server . sockets . sockets . size ?? 0 ;
31+ // TODO: add more metrics in the future
32+ // const subscriptions: Record<string, number> = {};
33+
34+ // this.server.sockets.adapter.rooms.forEach((socketsSet, roomName) => {
35+ // subscriptions[roomName] = socketsSet.size;
36+ // });
37+
38+ this . eventEmitter . emit ( MetricsEvents . SetWebsocketMetrics , {
39+ connectedClients,
40+ } ) ;
41+ }
42+
1943 @Cron ( '*/6 * * * * *' )
2044 @Lock ( { name : 'Push transactions to subscribers' , verbose : true } )
2145 async handleTransactionsUpdate ( ) {
0 commit comments