@@ -3,6 +3,7 @@ import type { IncomingMessage } from 'http';
33
44import { Presence } from '@rocket.chat/core-services' ;
55import type { ISocketConnection } from '@rocket.chat/core-typings' ;
6+ import { throttle } from 'underscore' ;
67import { v1 as uuidv1 } from 'uuid' ;
78import type WebSocket from 'ws' ;
89
@@ -74,7 +75,17 @@ export class Client extends EventEmitter {
7475
7576 public userToken ?: string ;
7677
77- private _seenPacket = true ;
78+ private updatePresence = throttle (
79+ ( ) => {
80+ if ( this . userId ) {
81+ void Presence . updateConnection ( this . userId , this . connection . id ) . catch ( ( err ) => {
82+ console . error ( 'Error updating connection presence:' , err ) ;
83+ } ) ;
84+ }
85+ } ,
86+ TIMEOUT ,
87+ { leading : true , trailing : false } ,
88+ ) ;
7889
7990 constructor (
8091 public ws : WebSocket ,
@@ -182,18 +193,6 @@ export class Client extends EventEmitter {
182193 this . ws . close ( WS_ERRORS . TIMEOUT , WS_ERRORS_MESSAGES . TIMEOUT ) ;
183194 } ;
184195
185- private messageReceived = ( ) : void => {
186- if ( this . _seenPacket || ! this . userId ) {
187- this . _seenPacket = true ;
188- return ;
189- }
190-
191- this . _seenPacket = true ;
192- void Presence . updateConnection ( this . userId , this . connection . id ) . catch ( ( err ) => {
193- console . error ( 'Error updating connection presence after heartbeat:' , err ) ;
194- } ) ;
195- } ;
196-
197196 ping ( id ?: string ) : void {
198197 this . send ( server . serialize ( { [ DDP_EVENTS . MSG ] : DDP_EVENTS . PING , ...( id && { [ DDP_EVENTS . ID ] : id } ) } ) ) ;
199198 }
@@ -203,9 +202,6 @@ export class Client extends EventEmitter {
203202 }
204203
205204 handleIdle = ( ) : void => {
206- if ( this . userId ) {
207- this . _seenPacket = false ;
208- }
209205 this . ping ( ) ;
210206 this . timeout = setTimeout ( this . closeTimeout , TIMEOUT ) ;
211207 } ;
@@ -218,7 +214,7 @@ export class Client extends EventEmitter {
218214 handler = async ( payload : WebSocket . Data , isBinary : boolean ) : Promise < void > => {
219215 try {
220216 const packet = server . parse ( payload , isBinary ) ;
221- this . messageReceived ( ) ;
217+ this . updatePresence ( ) ;
222218 this . emit ( 'message' , packet ) ;
223219 if ( this . wait ) {
224220 return new Promise ( ( resolve ) => this . once ( DDP_EVENTS . LOGGED , ( ) => resolve ( this . process ( packet . msg , packet ) ) ) ) ;
0 commit comments