A client for interacting with the ComfyUI API server using WebSockets.

NOTE: CORS policy: Request header field comfy-user is not allowed by Access-Control-Allow-Headers in preflight response. Please config.use empty string in browser.

Example

const client = new WsClient({
api_host: "YOUR_API_HOST"
});

// Connect to the server
client.connect();

// Listen for status updates
client.on("status", (status) => {
console.log("Status:", status);
});

// when done, close the client
client.close();

Hierarchy (view full)

Constructors

Properties

WebSocket: {
    CLOSED: 3;
    CLOSING: 2;
    CONNECTING: 0;
    OPEN: 1;
    prototype: WebSocket;
    new (url, protocols?): WebSocket;
}

Type declaration

    • new (url, protocols?): WebSocket
    • Parameters

      • url: string | URL
      • Optional protocols: string | string[]

      Returns WebSocket

  • Readonly CLOSED: 3
  • Readonly CLOSING: 2
  • Readonly CONNECTING: 0
  • Readonly OPEN: 1
  • prototype: WebSocket
_polling_interval: number = 1000
_polling_timer: any = null
api_base: string
api_host: string
clientId?: string
closed: boolean = false
events: EventEmitter<ComfyUIClientEvents & Record<string & {}, any>, any> = ...
fetch: {
    (input, init?): Promise<Response>;
    (input, init?): Promise<Response>;
}

Type declaration

    • (input, init?): Promise<Response>
    • Parameters

      • input: URL | RequestInfo
      • Optional init: RequestInit

      Returns Promise<Response>

    • (input, init?): Promise<Response>
    • Parameters

      • input: string | URL | Request
      • Optional init: RequestInit

      Returns Promise<Response>

socket?: null | WebSocket
socket_callbacks: Record<string, any> = {}
ssl: boolean
user: string
DEFAULT_API_BASE: string = ""
DEFAULT_API_HOST: string = "127.0.0.1:8188"
DEFAULT_USER: string = ""
IS_BROWSER: boolean = ...

Accessors

  • get registered(): (string & {} | (keyof ComfyUIClientEvents))[]
  • Returns (string & {} | (keyof ComfyUIClientEvents))[]

Methods

  • Disconnects the WebSocket connection and cleans up event listeners.

    Returns void

    This function does not return anything.

  • Adds an event listener for the specified event type.

    Type Parameters

    Parameters

    • type: T

      The type of event to listen for.

    • callback: ((...args) => void)

      The callback function to be executed when the event is triggered.

    • Optional options: any

      (Optional) Additional options for the event listener.

    Returns (() => void)

    A function that removes the event listener when called.

      • (): void
      • Returns void

  • Type Parameters

    • K extends keyof WebSocketEventMap

    Parameters

    • socket: WebSocket
    • type: K
    • listener: ((this, ev) => any)
        • (this, ev): any
        • Parameters

          • this: WebSocket
          • ev: WebSocketEventMap[K]

          Returns any

    • Optional options: boolean | AddEventListenerOptions

    Returns (() => void)

      • (): void
      • Returns void

  • Returns the headers for the API request.

    Parameters

    • Optional options: RequestInit

      (Optional) Additional options for the request.

    Returns HeadersInit

    The headers for the API request.

  • Generates the URL for the API endpoint based on the provided route.

    Parameters

    • route: string

      The route for the API endpoint.

    Returns string

    The generated URL for the API endpoint.

  • Connects to the WebSocket server by creating a new socket connection.

    Parameters

    • options: {
          polling?: {
              enabled: boolean;
              interval?: number;
          };
          websocket?: {
              enabled: boolean;
          };
      } = {}

      The options for connecting to the server.

      • Optional polling?: {
            enabled: boolean;
            interval?: number;
        }

        The options for polling.

        • enabled: boolean
        • Optional interval?: number
      • Optional websocket?: {
            enabled: boolean;
        }

        The options for the WebSocket connection.

        • enabled: boolean

    Returns WsClient

    • The instance of the class.
  • Creates and connects a WebSocket for realtime updates

    Parameters

    • isReconnect: boolean = false

      If the socket is connection is a reconnect attempt

    Returns void

  • Disconnects the WebSocket connection and cleans up event listeners.

    Returns void

  • Fetches API data based on the provided route and options.

    NOTE: CORS policy: Request header field comfy-user is not allowed by Access-Control-Allow-Headers in preflight response. Please use empty string in browser.

    Parameters

    • route: string

      The route for the API request.

    • Optional options: RequestInit

      (Optional) Additional options for the request.

    Returns Promise<Response>

    A promise that resolves to the API response.

  • Initializes sockets and realtime updates

    Returns void

    Deprecated

    move to client.connect()

  • Adds an event listener for the specified event type.

    Type Parameters

    Parameters

    • type: T

      The type of event to listen for.

    • callback: ((...args) => void)

      The callback function to be executed when the event is triggered.

    • Optional options: any

      (Optional) Additional options for the event listener.

    Returns (() => void)

    A function that removes the event listener when called.

      • (): void
      • Returns void

  • Adds an event listener for the specified event type.

    Type Parameters

    Parameters

    • type: T

      The type of event to listen for.

    • callback: ((...args) => void)

      The callback function to be executed when the event is triggered.

    • Optional options: any

      (Optional) Additional options for the event listener.

    Returns (() => void)

    A function that removes the event listener when called.

      • (): void
      • Returns void

  • Removes all event listeners from the given WebSocket and clears the socket_callbacks object.

    Returns void

  • Poll status for colab and other things that don't support websockets.

    Returns void

  • Generates a URL for viewing a specific file with the given filename, subfolder, and type.

    Parameters

    • filename: string

      The name of the file to view.

    • subfolder: string

      The subfolder where the file is located.

    • type: string

      The type of the file.

    Returns string

    The URL for viewing the file.

  • Generates the WebSocket URL based on the current API host and SSL configuration.

    Returns string

    The generated WebSocket URL.

  • Parameters

    • buf: ArrayBuffer

    Returns {
        image: ArrayBuffer;
        mime: any;
    }

    • image: ArrayBuffer
    • mime: any