Skip to content

Commit

Permalink
Merge pull request #627 from encorelab/develop
Browse files Browse the repository at this point in the history
Fix the bug getting econnreset error on production server
  • Loading branch information
markiianbabiak authored Oct 28, 2024
2 parents 9672dec + 667ec26 commit b4c5347
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
8 changes: 7 additions & 1 deletion backend/src/socket/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class Socket {
methods: ['GET', 'POST'],
},
adapter: createAdapter(redis.getPublisher, redis.getSubscriber),
pingTimeout: 60000, // 60 seconds (adjust as needed)
pingInterval: 25000, // 25 seconds (adjust as needed)
});

this._io = io;
Expand Down Expand Up @@ -80,11 +82,15 @@ class Socket {
});

socket.on('disconnect', () => {
console.warn(`Socket ${socket.id} disconnected:`);

// 1. Leave all rooms
const rooms = socket.rooms;
rooms.forEach((room) => {
socket.leave(room);
// Potentially update or notify the room of the disconnect
});

// 2. Remove from SocketManager
this._socketManager.removeBySocketId(socket.id);
});

Expand Down
27 changes: 23 additions & 4 deletions frontend/src/app/services/socket.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable } from '@angular/core';
import { Socket } from 'ngx-socket-io';
import { Observable, Subscription, take } from 'rxjs';
import { Observable, Subscription, take, catchError, of } from 'rxjs';
import { SocketPayload } from '../models/socketPayload';
import { SocketEvent } from '../utils/constants';
import { TraceService } from './trace.service';
Expand All @@ -9,6 +9,10 @@ import { TraceService } from './trace.service';
providedIn: 'root',
})
export class SocketService {
private retryCount = 0;
private maxRetries = 10;
private retryInterval = 5000;

constructor(private socket: Socket, private traceService: TraceService) {}

/**
Expand All @@ -19,6 +23,10 @@ export class SocketService {
*/
connect(userID: string, boardID: string): void {
this.socket.emit('join', userID, boardID);

this.socket.on('connect_error', (error: Error) => {
console.error('Socket connection error:', error);
});
}

/**
Expand All @@ -29,7 +37,12 @@ export class SocketService {
* @returns subscription object to unsubscribe from in the future
*/
listen(event: SocketEvent, handler: Function): Subscription {
const observable = this.socket.fromEvent<any>(event);
const observable = this.socket.fromEvent<any>(event).pipe(
catchError((error: Error) => {
console.error(`Error handling event ${event}:`, error);
return of(null); // Return a default value
})
);
return observable.subscribe((value) => handler(value));
}

Expand All @@ -41,8 +54,14 @@ export class SocketService {
* @returns subscription object to unsubscribe from in the future
*/
once(event: SocketEvent, handler: Function): Subscription {
const observable = this.socket.fromEvent<any>(event);
return observable.pipe(take(1)).subscribe((value) => handler(value)); // Use take(1) to complete after the first emission
const observable = this.socket.fromEvent<any>(event).pipe(
take(1),
catchError((error: Error) => {
console.error(`Error handling event ${event}:`, error);
return of(null); // Return a default value
})
);
return observable.subscribe((value) => handler(value));
}

/**
Expand Down

0 comments on commit b4c5347

Please sign in to comment.