mirror of
https://github.com/Dokploy/dokploy.git
synced 2026-07-26 08:15:25 +02:00
feat(api): implement lazy WebSocket client for improved connection management
- Introduced a `createLazyWSClient` function to manage WebSocket connections more efficiently by delaying the creation of the client until it's needed. - Updated the `wsClient` initialization to use the new lazy client, enhancing performance and resource management in the application.
This commit is contained in:
@@ -30,12 +30,41 @@ const getWsUrl = () => {
|
|||||||
return `${protocol}${host}/drawer-logs`;
|
return `${protocol}${host}/drawer-logs`;
|
||||||
};
|
};
|
||||||
|
|
||||||
const wsClient =
|
// Create WebSocket client with delayed connection
|
||||||
typeof window !== "undefined"
|
const createLazyWSClient = () => {
|
||||||
? createWSClient({
|
if (typeof window === "undefined") return null;
|
||||||
url: getWsUrl() || "",
|
|
||||||
})
|
let actualClient: ReturnType<typeof createWSClient> | null = null;
|
||||||
: null;
|
|
||||||
|
return {
|
||||||
|
request: (op: any, callbacks: any) => {
|
||||||
|
if (!actualClient) {
|
||||||
|
const wsUrl = getWsUrl();
|
||||||
|
if (wsUrl) {
|
||||||
|
actualClient = createWSClient({ url: wsUrl });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return actualClient?.request(op, callbacks) || (() => {});
|
||||||
|
},
|
||||||
|
close: () => {
|
||||||
|
if (actualClient) {
|
||||||
|
actualClient.close();
|
||||||
|
actualClient = null;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
getConnection: () => {
|
||||||
|
if (!actualClient) {
|
||||||
|
const wsUrl = getWsUrl();
|
||||||
|
if (wsUrl) {
|
||||||
|
actualClient = createWSClient({ url: wsUrl });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return actualClient!.getConnection();
|
||||||
|
},
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
const wsClient = createLazyWSClient();
|
||||||
|
|
||||||
/** A set of type-safe react-query hooks for your tRPC API. */
|
/** A set of type-safe react-query hooks for your tRPC API. */
|
||||||
export const api = createTRPCNext<AppRouter>({
|
export const api = createTRPCNext<AppRouter>({
|
||||||
|
|||||||
Reference in New Issue
Block a user