fix: retries,acktimeout configuration socketio

This commit is contained in:
abdou6666 2025-05-01 16:48:03 +01:00
parent beddb7f22a
commit 7b5e0a1aa6
6 changed files with 65 additions and 25 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright © 2024 Hexastack. All rights reserved.
* Copyright © 2025 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
@ -8,6 +8,8 @@
import type { NextApiRequest, NextApiResponse } from "next";
import { parseEnvBoolean, parseEnvNumber } from "@/utils/env";
type ResponseData = {
apiUrl: string;
ssoEnabled: boolean;
@ -18,11 +20,15 @@ export default function handler(
req: NextApiRequest,
res: NextApiResponse<ResponseData>,
) {
res.status(200).json({
const MB = 1024 * 1024;
const config: ResponseData = {
apiUrl: process.env.NEXT_PUBLIC_API_ORIGIN || "http://localhost:4000",
ssoEnabled: process.env.NEXT_PUBLIC_SSO_ENABLED === "true" || false,
maxUploadSize: process.env.UPLOAD_MAX_SIZE_IN_BYTES
? Number(process.env.UPLOAD_MAX_SIZE_IN_BYTES)
: 20 * 1024 * 1024, // 20 MB in bytes
});
ssoEnabled: parseEnvBoolean(process.env.NEXT_PUBLIC_SSO_ENABLED, false),
maxUploadSize: parseEnvNumber(
process.env.UPLOAD_MAX_SIZE_IN_BYTES,
20 * MB,
),
};
res.status(200).json(config);
}

27
frontend/src/utils/env.ts Normal file
View File

@ -0,0 +1,27 @@
/*
* Copyright © 2025 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
*/
export const parseEnvNumber = (
value: string | undefined,
fallback: number,
): number => {
const parsed = Number(value);
return isNaN(parsed) ? fallback : parsed;
};
// Utility to parse environment variable as boolean
export const parseEnvBoolean = (
value: string | undefined,
fallback: boolean,
): boolean => {
if (typeof value !== "string") {
return fallback;
}
return value.toLowerCase() === "true";
};

View File

@ -1,12 +1,12 @@
/*
* Copyright © 2024 Hexastack. All rights reserved.
* Copyright © 2025 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
*/
import { io, Socket, ManagerOptions, SocketOptions } from "socket.io-client";
import { io, ManagerOptions, Socket, SocketOptions } from "socket.io-client";
import { IOIncomingMessage, IOOutgoingMessage } from "./types/io-message";
@ -19,19 +19,19 @@ export class SocketIoClient {
*/
static defaultConfig: SocketIoClientConfig = {
// Socket options
ackTimeout: 1000,
// auth: undefined,
retries: 3,
// Manager options
autoConnect: true,
// parser: undefined,
// randomizationFactor:0.5,
randomizationFactor: 0.5,
reconnection: true,
reconnectionAttempts: 100,
reconnectionDelay: 1000,
reconnectionDelayMax: 5000,
timeout: 20000,
retries: 0,
ackTimeout: 15_000,
// Low Level Options
addTrailingSlash: true, // eg: https://domain.path/ => https://domain.path/

View File

@ -293,17 +293,23 @@ const ChatProvider: React.FC<{
: OutgoingMessageState.sending,
);
setMessage("");
const sentMessage = await socketCtx.socket.post<TMessage>(
`/webhook/${config.channel}/`,
{
data: {
...data,
author: data.author ?? participants[1].id,
try {
// when the request timeout it throws exception & break frontend
const sentMessage = await socketCtx.socket.post<TMessage>(
`/webhook/${config.channel}/`,
{
data: {
...data,
author: data.author ?? participants[1].id,
},
},
},
);
);
handleNewIOMessage(sentMessage.body);
handleNewIOMessage(sentMessage.body);
} catch (error) {
// eslint-disable-next-line no-console
console.error("Unable to subscribe user", error);
}
};
const handleSubscription = useCallback(
async (firstName?: string, lastName?: string) => {

View File

@ -1,5 +1,5 @@
/*
* Copyright © 2024 Hexastack. All rights reserved.
* Copyright © 2025 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.

View File

@ -29,19 +29,19 @@ export class SocketIoClient {
*/
static defaultConfig: SocketIoClientConfig = {
// Socket options
ackTimeout: 1000,
// auth: undefined,
retries: 3,
// Manager options
autoConnect: true,
// parser: undefined,
// randomizationFactor:0.5,
randomizationFactor: 0.5,
reconnection: true,
reconnectionAttempts: 100,
reconnectionDelay: 1000,
reconnectionDelayMax: 5000,
timeout: 20000,
retries: 0,
ackTimeout: 15_000,
// Low Level Options
addTrailingSlash: true, // eg: https://domain.path/ => https://domain.path/
@ -154,6 +154,7 @@ export class SocketIoClient {
if (response.statusCode >= 200 && response.statusCode < 300) {
return response;
}
throw new Error(
`Request failed with status code ${response.statusCode}: ${JSON.stringify(
response.body,