Skip to content

Commit d4175f1

Browse files
committed
refactor: enhance WebSocket handling and client cleanup
- Updated `BaseInternalClientService` to support WebSocket subscriptions over Unix sockets, improving connection handling. - Added logging for WebSocket URI creation to aid in debugging. - Enhanced `clearClient` method to properly dispose of the WebSocket client, ensuring resource cleanup. - Added comments for clarity on client stopping processes in `CliInternalClientService` and `InternalClientService`.
1 parent 879d804 commit d4175f1

4 files changed

Lines changed: 32 additions & 11 deletions

File tree

api/src/unraid-api/cli/internal-client.service.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export class CliInternalClientService {
5656
}
5757

5858
public clearClient() {
59+
// Stop the Apollo client to terminate any active processes
5960
this.client?.stop();
6061
this.client = null;
6162
}

api/src/unraid-api/shared/internal-graphql-client.factory.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,10 @@ export class InternalGraphQLClientFactory {
9696
const socketPath = this.getSocketPath();
9797
this.logger.debug('Creating GraphQL client using Unix socket: %s', socketPath);
9898
if (enableSubscriptions) {
99-
wsUri = 'ws://localhost/graphql';
99+
// For Unix sockets, use the ws+unix:// protocol
100+
// Format: ws+unix://socket/path:/url/path
101+
wsUri = `ws+unix://${socketPath}:/graphql`;
102+
this.logger.debug('WebSocket subscriptions over Unix socket: %s', wsUri);
100103
}
101104

102105
const agent = new Agent({

packages/unraid-api-plugin-connect/src/internal-rpc/internal.client.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export class InternalClientService {
5050
}
5151

5252
public clearClient() {
53+
// Stop the Apollo client to terminate any active processes
5354
this.client?.stop();
5455
this.client = null;
5556
}

packages/unraid-shared/src/services/base-internal-client.service.ts

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export interface InternalClientOptions {
3131
export abstract class BaseInternalClientService {
3232
protected readonly logger: Logger;
3333
protected client: ApolloClient<NormalizedCacheObject> | null = null;
34+
private wsClient: ReturnType<typeof createClient> | null = null;
3435

3536
private readonly PROD_NGINX_PORT = 80;
3637

@@ -156,16 +157,25 @@ export abstract class BaseInternalClientService {
156157

157158
// If subscriptions are enabled, set up WebSocket link
158159
if (this.options.enableSubscriptions) {
159-
const wsUri = this.isRunningOnSocket()
160-
? 'ws://localhost/graphql'
161-
: this.getApiAddress('ws');
162-
163-
const wsLink = new GraphQLWsLink(
164-
createClient({
165-
url: wsUri,
166-
connectionParams: () => ({ 'x-api-key': apiKey }),
167-
})
168-
);
160+
let wsUri: string;
161+
162+
if (this.isRunningOnSocket()) {
163+
// For Unix sockets, use the ws+unix:// protocol
164+
// Format: ws+unix://socket/path:/url/path
165+
const socketPath = this.getSocketPath();
166+
wsUri = `ws+unix://${socketPath}:/graphql`;
167+
this.logger.debug('Enabling WebSocket subscriptions over Unix socket: %s', wsUri);
168+
} else {
169+
wsUri = this.getApiAddress('ws');
170+
this.logger.debug('Enabling WebSocket subscriptions at: %s', wsUri);
171+
}
172+
173+
this.wsClient = createClient({
174+
url: wsUri,
175+
connectionParams: () => ({ 'x-api-key': apiKey }),
176+
});
177+
178+
const wsLink = new GraphQLWsLink(this.wsClient);
169179

170180
const splitLink = split(
171181
({ query }) => {
@@ -214,7 +224,13 @@ export abstract class BaseInternalClientService {
214224
}
215225

216226
public clearClient() {
227+
// Stop the Apollo client to terminate any active processes
217228
this.client?.stop();
229+
// Clean up WebSocket client if it exists
230+
if (this.wsClient) {
231+
this.wsClient.dispose();
232+
this.wsClient = null;
233+
}
218234
this.client = null;
219235
}
220236
}

0 commit comments

Comments
 (0)