http-sync

command
v1.61.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 3, 2026 License: MIT Imports: 17 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

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.

Documentation

Overview

Command http-sync syncs documents over plain HTTP requests: no WebSocket, for scripts, cron jobs and serverless functions.

go run ./examples/http-sync

It starts a ygo server with HTTPSyncHandler on a loopback port, guarded by a bearer token, and two Go peers (client.HTTPSync) that edit offline and converge, while requests without the token are refused. With -serve it keeps the server running for curl (see README.md), and -print-update TEXT writes a V1 update that appends TEXT, for curl's --data-binary.

yserve -http-sync serves the same without writing code.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL