Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -219,14 +219,7 @@ const root: FastifyPluginAsync = async (fastify): Promise<void> => {
userAddress,
tokenAddresses,
send: (data: string) => {
try {
reply.raw.write(data);
} catch (error) {
fastify.log.error(
`Error sending SSE data to client ${clientId}:`,
error
);
}
reply.raw.write(data);
},
close: () => {
try {
Expand Down Expand Up @@ -259,9 +252,13 @@ const root: FastifyPluginAsync = async (fastify): Promise<void> => {
);
}

// Handle client disconnect
request.raw.on('close', async () => {
fastify.log.info(`Client ${clientId} closed connection`);
let isDisconnecting = false;
const handleDisconnect = async (reason: string) => {
if (isDisconnecting) {
return;
}
isDisconnecting = true;
fastify.log.info(`Client ${clientId} disconnected (${reason})`);
sseService.removeClient(clientId);

// Stop tracking if no other clients are connected for this user
Expand Down Expand Up @@ -296,20 +293,24 @@ const root: FastifyPluginAsync = async (fastify): Promise<void> => {
fastify.log.error(error, 'Error updating tracked tokens');
}
}
});
};

// Send keep-alive messages every 30 seconds
const keepAliveInterval = setInterval(() => {
try {
sseClient.send('event: ping\ndata: {}\n\n');
} catch (error) {
const didSend = sseService.sendToClient(
clientId,
'event: ping\ndata: {}\n\n'
);
if (!didSend) {
clearInterval(keepAliveInterval);
void handleDisconnect('ping failed');
}
}, 30000);

// Clean up interval when connection closes
// Handle client disconnect
request.raw.on('close', () => {
clearInterval(keepAliveInterval);
void handleDisconnect('close');
});

// Don't end the response - keep it open for SSE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,11 +327,7 @@ export class BalanceTrackingServiceMain implements BalanceTrackingService {
}

// Check for allowance changes (if both have allowance data)
if (
lastBalance.allowance &&
currentBalance.allowance &&
lastBalance.allowance !== currentBalance.allowance
) {
if (lastBalance.allowance !== currentBalance.allowance) {
const event: BalanceAllowanceChangeEvent = {
type: 'allowance_change',
chainId,
Expand Down
4 changes: 2 additions & 2 deletions libs/services/src/SSEService/SSEService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export interface SSEService {
* @param clientId
* @param data
*/
sendToClient(clientId: string, data: string): void;
sendToClient(clientId: string, data: string): boolean;

/**
* Return the SSE client subscriptions for a given user account and chain
Expand Down Expand Up @@ -65,7 +65,7 @@ export interface SSEService {
): void;

/**
* Push some update in the balance ch
* Push a balance update event to the clients
* @param event
*/
broadcastBalanceUpdate(event: BalanceAllowanceChangeEvent): void;
Expand Down
8 changes: 6 additions & 2 deletions libs/services/src/SSEService/SSEServiceMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,22 @@ export class SSEServiceMain implements SSEService {
);
}

sendToClient(clientId: string, data: string): void {
sendToClient(clientId: string, data: string): boolean {
const client = this.clients.get(clientId);
if (client) {
try {
client.send(data);
return true;
} catch (error) {
logger.error(
`Error sending data to SSE client ${client.clientId}:`,
error
);
this.removeClient(client.clientId);
return false;
}
}
return false;
}

broadcastToUser(
Expand Down Expand Up @@ -108,7 +111,8 @@ export class SSEServiceMain implements SSEService {

// Cleanup method to remove all clients (useful for graceful shutdown)
cleanup(): void {
this.clients.forEach((client, id) => {
const clientIds = Array.from(this.clients.keys());
clientIds.forEach((id) => {
this.removeClient(id);
});
}
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@
"@types/node-telegram-bot-api": "^0.64.6",
"@types/pg": "^8.11.13",
"@types/tap": "^15.0.5",
"@types/uuid": "^10.0.0",
"@typescript-eslint/eslint-plugin": "^5.58.0",
"@typescript-eslint/parser": "^5.58.0",
"concurrently": "^7.0.0",
Expand Down
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3236,11 +3236,6 @@
resolved "https://registry.yarnpkg.com/@types/trusted-types/-/trusted-types-2.0.7.tgz#baccb07a970b91707df3a3e8ba6896c57ead2d11"
integrity sha512-ScaPdn1dQczgbl0QFTeTOmVHFULt394XJgOQNoyVhZ6r2vLnMLJfBPd53SB52T/3G36VI1/g2MZaX0cwDuXsfw==

"@types/uuid@^10.0.0":
version "10.0.0"
resolved "https://registry.yarnpkg.com/@types/uuid/-/uuid-10.0.0.tgz#e9c07fe50da0f53dc24970cca94d619ff03f6f6d"
integrity sha512-7gqG38EyHgyP1S+7+xomFtL+ZNHcKv6DwNaCZmJmo1vgMugyF3TCnXVg4t1uk89mLNwnLtnY3TpOpCOyp1/xHQ==

"@types/yargs-parser@*":
version "21.0.0"
resolved "https://registry.npmjs.org/@types/yargs-parser/-/yargs-parser-21.0.0.tgz"
Expand Down