Skip to content
SumOfficeSumOfficeSumOffice

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.

Terminal window
cd ~/Desktop/SumDoc-API-first-run
node a4-websocket-demo.mjs
  1. Start fastdoc-cli serve --ws 0 as a child process. Zero means “any free port”.
  2. Read the first line of stdout — it has host, port, token, and schema.
  3. Connect to ws://127.0.0.1:<port>/?token=<token>, substituting the values from that line.
  4. Wait for the session greeting with the fastdoc.session-serve.v1 schema.
  5. Send the same JSON objects as over JSONL.
  6. When closing the application, send shutdown first, then close the socket and the child process.
1. WebSocket: ws://127.0.0.1:52601
2. Session protocol: fastdoc.session-serve.v1
3. Ping: true
4. 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.docx
7. Reopened from disk: WS API DEMO: This is a sample word document. It has two pages, but no headers or footers.
RESULT: PASS
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" }));

Documentation assistant

Answers are assembled from the documentation and may be inaccurate — check the sources.