messages - chrisgoringe/Comfy-Custom-Node-How-To GitHub Wiki

Send a message from Python to JavaScript

In python, just use the Prompt Server to send a message with a unique handle. Messages are dictionaries of JSON serializable things.

from server import PromptServer
dictionary_of_stuff = {"something":"A text message"}
PromptServer.instance.send_sync("my-message-handle", dictionary_of_stuff)

In the JavaScript, add an event listener for your unique handle, and provide it with a method to call with the event. The dictionary sent can be found in event.detail.

import { app } from "../../../scripts/app.js";
def myMessageHandler(event) {
    alert(event.detail.something);
}
// in setup()
api.addEventListener("my-message-handle", myMessageHandler);

For more, take a look at the worked example here: Passing control to javascript