http-sync
Sync documents with plain HTTP requests, for clients that cannot keep a
WebSocket open: scripts, cron jobs, serverless functions.
go run ./examples/http-sync
sync A: pulled changes false, now "Alice's line. "
sync B: pulled changes true, now "Alice's line. Bob's line. "
sync A: pulled changes true, now "Alice's line. Bob's line. "
converged: "Alice's line. Bob's line. "
poll with nothing new: pulled changes false
without the token: GET 401, POST 401, document unchanged
The program starts a ygo server with HTTPSyncHandler on a loopback port, guarded by a bearer token in OnHTTPRequest (without that hook every HTTP request is refused). Two Go peers (client.HTTPSync) edit offline and converge:
- each
Sync pulls what the server has and the document lacks, then pushes what the document has and the server lacks;
- two peers that both edited offline need three syncs (A, B, A): B's sync brings A's edit to B, A's second brings B's to A;
- a poll of an unchanged document costs a 304 (
HTTPSync sends the last ETag);
- requests without the token, or with a wrong one, are refused and change nothing.
The same documents are open to WebSocket clients when the server's Handler is mounted too: an HTTP write reaches them at once.
With curl
go run ./examples/http-sync -serve & # loopback, port 8090
go run ./examples/http-sync -print-update "Hello from curl. " > u.bin
curl --data-binary @u.bin -H 'Content-Type: application/octet-stream' \
-H 'Authorization: Bearer example-token' http://127.0.0.1:8090/sync/notes
curl -s -H 'Authorization: Bearer example-token' http://127.0.0.1:8090/sync/notes -o state.bin
A GET returns the document as one V1 update (?sv=<base64 state vector> returns only what that state lacks; v=2 returns V2), with its state vector in the Ygo-State-Vector header. A POST body is one V1 update. See docs/yserve.md for every parameter.
Without code
yserve -http-sync serves the same at the documents' own URLs:
YSERVE_HTTP_SYNC_TOKEN=... yserve -store ygo.db -http-sync
Its token guards HTTP only: yserve's WebSocket endpoint on the same URLs takes no token.