Class: ComfyUI
helpers/services/comfyui.ComfyUI
Manages the lifecycle and interactions of the ComfyUI server, including initiating server processes, managing WebSocket connections for clients, and handling workflow queueing and system status checks.
Table of contents
Properties
Accessors
Methods
Properties
clients
• clients: Map
<string
, WebSocket
>
Defined in
src/electron/helpers/services/comfyui.ts:56 (opens in a new tab)
instance
▪ Static
instance: null
| ComfyUI
= null
Defined in
src/electron/helpers/services/comfyui.ts:51 (opens in a new tab)
Accessors
getInstance
• get
getInstance(): ComfyUI
Retrieves the singleton instance of the ComfyUI class.
Returns
The singleton instance.
Throws
Throws an error if the singleton has not been initialized before calling this getter.
Defined in
src/electron/helpers/services/comfyui.ts:391 (opens in a new tab)
Methods
free
▸ free(options
): Promise
<boolean
>
Frees resources in the ComfyUI application by unloading models and/or freeing memory. This method makes a POST request to the server to either unload models or free memory based on the provided parameters.
Parameters
Name | Type | Description |
---|---|---|
options | Object | Configuration options for freeing resources. |
options.freeMemory? | boolean | Optional flag to specify whether to free up memory resources. |
options.unloadModels? | boolean | Optional flag to specify whether to unload the models loaded in memory. |
Returns
Promise
<boolean
>
- Returns true if the operation was successful (HTTP status 200), otherwise false.
Example
const wasFreed = await comfyUI.getInstance.free({ unloadModels: true, freeMemory: true });
console.log(`Resources freed: ${wasFreed}`);
Defined in
src/electron/helpers/services/comfyui.ts:304 (opens in a new tab)
queueWorkflow
▸ queueWorkflow(prompt
, clientId
): Promise
<undefined
| QueueWorkflowResponse
>
Queues a new workflow for processing by the ComfyUI server.
Parameters
Name | Type | Description |
---|---|---|
prompt | NodeChain | The workflow data to be processed. |
clientId | string | The client ID associated with this request. |
Returns
Promise
<undefined
| QueueWorkflowResponse
>
The response from the server including prompt ID and any node errors.
Throws
Throws an error if the request to queue the workflow fails.
Defined in
src/electron/helpers/services/comfyui.ts:365 (opens in a new tab)
registerClient
▸ registerClient<T
>(clientId
, handleMessage
): void
Registers a new client for WebSocket communications, setting up a new WebSocket connection if one does not already exist for the given client ID.
Type parameters
Name |
---|
T |
Parameters
Name | Type | Description |
---|---|---|
clientId | string | The unique identifier for the client. |
handleMessage | WebSocketMessageHandler <T > | The function to handle incoming WebSocket messages. |
Returns
void
Defined in
src/electron/helpers/services/comfyui.ts:331 (opens in a new tab)
setupWebSocket
▸ setupWebSocket<T
>(clientId
, handleMessage
): void
Sets up a WebSocket connection for a given client ID and attaches message handling. It manages the lifecycle events of the WebSocket connection including opening, messages, closing, and errors.
Type parameters
Name |
---|
T |
Parameters
Name | Type | Description |
---|---|---|
clientId | string | The unique identifier for the client. |
handleMessage | WebSocketMessageHandler <T > | The function to call when a message is received on the WebSocket. |
Returns
void
Defined in
src/electron/helpers/services/comfyui.ts:231 (opens in a new tab)
unregisterClient
▸ unregisterClient(clientId
): Object
Unregisters a client by closing its WebSocket connection and removing it from the internal client list. If the client does not exist, it returns an indication of non-existence.
Parameters
Name | Type | Description |
---|---|---|
clientId | string | The unique identifier of the client to unregister. |
Returns
Object
An object indicating whether the client was closed or did not exist.
Name | Type |
---|---|
closed? | boolean |
notExist? | boolean |
Defined in
src/electron/helpers/services/comfyui.ts:346 (opens in a new tab)
uploadImage
▸ uploadImage(appId
, blob
): Promise
<void
>
Uploads an image to the ComfyUI server. This method uses FormData to append the image Blob along with metadata and then makes a POST request to upload the image. It handles both success and error scenarios, logging the error if the upload fails.
Parameters
Name | Type | Description |
---|---|---|
appId | string | The application identifier used to uniquely name the image file. |
blob | Blob | The image data as a Blob, which will be sent to the server. |
Returns
Promise
<void
>
- Completes when the upload is done. If the upload fails, it logs the error.
Example
// Example of calling uploadImage
const imageBlob = new Blob([imageData], { type: 'image/png' });
comfyUI.uploadImage('myAppId', imageBlob)
.then(() => console.log('Image uploaded successfully'))
.catch(error => console.error('Failed to upload image', error));
Throws
- Throws an error if the axios POST request fails to send the image.
Defined in
src/electron/helpers/services/comfyui.ts:274 (opens in a new tab)
init
▸ init(options
): Promise
<ComfyUIStatus
>
Initializes the ComfyUI service as a singleton instance. This method configures the server, checks its readiness, and starts it if not already running.
Parameters
Name | Type | Default value | Description |
---|---|---|---|
options | Object | undefined | Configuration options for initializing ComfyUI. |
options.debug? | boolean | false | Enables or disables debug logging. |
options.host? | string | "127.0.0.1" | The host address of the ComfyUI server. |
options.maxRetries? | number | 60 | The maximum number of retries for checking server readiness. |
options.port? | number | 1337 | The port of the ComfyUI server. |
options.timeout? | number | 2000 | The timeout in milliseconds for checking server readiness. |
Returns
Promise
<ComfyUIStatus
>
The status of the ComfyUI server.
Throws
Throws an error if the server fails to start or is not ready after maximum retries.
Defined in
src/electron/helpers/services/comfyui.ts:81 (opens in a new tab)