Metadata-Version: 2.5
Name: comfy-peer-host
Version: 0.1.0
Summary: Headless collaboration host for Comfy UI Web: lets people build and run ComfyUI graphs on this machine over WebRTC (PeerJS), no browser needed.
Project-URL: Homepage, https://github.com/mastashake08/comfy-peer-host
Project-URL: Repository, https://github.com/mastashake08/comfy-peer-host
Project-URL: Issues, https://github.com/mastashake08/comfy-peer-host/issues
Project-URL: Web app, https://mastashake08.github.io/comfy-ui-web/
Keywords: collaboration,comfyui,peerjs,stable-diffusion,webrtc
Classifier: Development Status :: 4 - Beta
Classifier: Environment :: Console
Classifier: Framework :: AsyncIO
Classifier: Intended Audience :: End Users/Desktop
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Multimedia :: Graphics
Requires-Python: >=3.11
Requires-Dist: aiohttp>=3.9
Requires-Dist: aiortc>=1.9
Description-Content-Type: text/markdown

# comfy-peer-host

A headless host for [Comfy UI Web](https://github.com/mastashake08/comfy-ui-web) collaboration sessions.
Run it on the computer that has ComfyUI, and people can join from their browser with an invite link.
They build the graph together and run it on this computer's ComfyUI, even when nobody has the web app open here.

It speaks the same peer-to-peer protocol as the web app's own **Collaborate** host (WebRTC over [PeerJS](https://peerjs.com)), so guests can't tell the difference:

- Guests get this server's node list and the shared graph, and their edits sync live between everyone.
- Anyone can press **Queue**. The prompt runs on this machine's ComfyUI, and progress, live previews, errors and result images stream back to every guest.
- The shared graph is saved to disk, and the session ID (so the invite link) stays the same across restarts.

## Install and run

Requires Python 3.11+.

```bash
pipx install comfy-peer-host          # or: uv tool install comfy-peer-host
comfy-peer-host                       # ComfyUI at http://127.0.0.1:8188
comfy-peer-host --comfy-url http://127.0.0.1:8189 --name "Studio PC"
```

Or run it once without installing: `uvx comfy-peer-host`. From a source checkout: `uv run comfy-peer-host`.

It prints the invite link:

```
  Comfy UI Web host is running.
  Invite link:  https://mastashake08.github.io/comfy-ui-web/#join=cuw-txvwcd8gectw
```

Share the link, or have people paste the code into **Collaborate → Join** in the web app.

| Option | Default | |
| --- | --- | --- |
| `--comfy-url` | `http://127.0.0.1:8188` (or `$COMFY_URL`) | ComfyUI server to run prompts on |
| `--name` | `Host` | Name shown to guests |
| `--workflow FILE` | | Start the shared graph from a workflow saved in the web app (Workflow → Save) |
| `--state-dir` | `~/.comfy-peer-host` | Where the session ID and shared graph (`graph.json`) are kept |
| `--new-id` | | Make a new session ID; old invite links stop working |
| `--session-id cuw-…` | | Use a specific session ID |
| `--app-url` | the hosted web app | Web app used in the invite link |
| `--turn` | off | Also offer PeerJS's public TURN relay, for guests behind very strict networks |
| `--peerjs-host/-port/-path/-key`, `--insecure-signalling` | PeerJS cloud | Use your own PeerJS server |

ComfyUI needs no special flags: the host talks to it from the same machine.

### Keep it running (macOS)

Save this as `~/Library/LaunchAgents/com.comfy-peer-host.plist`, adjusting the paths, then run
`launchctl load ~/Library/LaunchAgents/com.comfy-peer-host.plist`:

```xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0"><dict>
  <key>Label</key><string>com.comfy-peer-host</string>
  <key>ProgramArguments</key><array>
    <string>/path/to/comfy-peer-host</string>  <!-- output of: which comfy-peer-host -->
    <string>--name</string><string>Studio PC</string>
  </array>
  <key>RunAtLoad</key><true/>
  <key>KeepAlive</key><true/>
  <key>StandardOutPath</key><string>/tmp/comfy-peer-host.log</string>
  <key>StandardErrorPath</key><string>/tmp/comfy-peer-host.log</string>
</dict></plist>
```

## Security

Anyone who has the invite link can edit the shared graph and run prompts on your GPU. Treat the link like a password.
Use `--new-id` to revoke it. Guests can't read your files: they only see the node list and the shared graph, and they can only queue prompts.

## How it works

- `peerjs.py`: a small PeerJS-compatible peer built on [aiortc](https://github.com/aiortc/aiortc). It registers with the PeerJS signalling server, answers guests' WebRTC offers, and uses PeerJS's "binary" data-channel format (BinaryPack, with messages over 16,300 bytes split into chunks).
- `binarypack.py`: a byte-for-byte port of `peerjs-js-binarypack`, tested against vectors produced by the JavaScript library.
- `comfy.py`: async ComfyUI HTTP and websocket client.
- `host.py`: the session protocol (welcome, graph ops, cursors, queueing, forwarding progress, previews and images), matching the web app's `src/lib/collab.js`.

## Development

```bash
uv run --group dev pytest
```

### Releasing

1. Bump `version` in `pyproject.toml` and commit.
2. Tag and push: `git tag v0.2.0 && git push origin v0.2.0`.

The **Release** workflow tests, checks the tag matches the version, builds the wheel and sdist, publishes them to
PyPI (trusted publishing, no token stored) and creates a GitHub Release with both files attached.

To regenerate the BinaryPack test vectors, run `tests/make_vectors.mjs` with Node from a `comfy-ui-web` checkout (it imports `peerjs-js-binarypack`).
