The same contract over WebSocket
Starting fastdoc-cli with WebSocket on a local address, the connection sequence with the pass, the minimal client loop, and how it differs from JSON-line exchange.
WebSocket is convenient when your product’s shell is already asynchronous and built on web technologies. The core picks a free local port itself and prints a single line with the connection address and a one-time pass.
cd ~/Desktop/SumDoc-API-first-runnode a4-websocket-demo.mjscd $HOME\Desktop\SumDoc-API-first-runnode a4-websocket-demo.mjscd ~/Desktop/SumDoc-API-first-runnode a4-websocket-demo.mjsConnection sequence
Section titled “Connection sequence”- Start
fastdoc-cli serve --ws 0as a child process. Zero means “any free port”. - Read the first line of stdout — it has
host,port,token, andschema. - Connect to
ws://127.0.0.1:<port>/?token=<token>, substituting the values from that line. - Wait for the session greeting with the
fastdoc.session-serve.v1schema. - Send the same JSON objects as over JSONL.
- When closing the application, send
shutdownfirst, then close the socket and the child process.
Actual result
Section titled “Actual result”1. WebSocket: ws://127.0.0.1:526012. Session protocol: fastdoc.session-serve.v13. Ping: true4. Before: This is a sample word document. It has two pages, but no headers or footers.5. After: WS API DEMO: This is a sample word document. It has two pages, but no headers or footers.6. Saved: /Users/tester/Desktop/SumDoc-API-first-run/api-demo-websocket-result.docx7. Reopened from disk: WS API DEMO: This is a sample word document. It has two pages, but no headers or footers.RESULT: PASSMinimal client loop
Section titled “Minimal client loop”const child = spawn(cli, ["serve", "--ws", "0"]);const bootstrap = JSON.parse(await firstLine(child.stdout));const ws = new WebSocket( `ws://${bootstrap.host}:${bootstrap.port}/?token=${bootstrap.token}`);await new Promise(resolve => ws.addEventListener("open", resolve, { once: true }));
ws.send(JSON.stringify({ id: "p1", cmd: "ping" }));