last commits
clone
- read-only
- https://git.nply.org/nply/liveside
liveside
A tiny live-reload server written in POSIX-first portable C99. It watches a directory tree
for changes and pushes the browser a Server-Sent Events (SSE) reload
message, which reloads the page.
Runs on macOS, Linux, the BSDs, and Windows. The main server code stays
POSIX-first (poll, dirent, unistd, BSD sockets, stat) and uses
TinyCThread
for portable threads/mutexes. On Win32 toolchains that do not provide POSIX
headers directly, use packages from win32ports.
Project-owned code under ports/ is reserved for reusable pieces that are core
to liveside and may be extracted later, such as socket smoothing,
ignore-file matching, and the native Windows watcher.
It is deliberately not a static file server — it only ever answers
two requests, /livereload (the SSE stream) and /liveside.js (the
client snippet). Your actual site keeps being served by whatever you
already use — darkhttpd, lighttpd, python3 -m http.server, anything
— on its own port. liveside just sits alongside it (hence the name).
It fills the same role as the autorefresh target in the
saikrupagas.com Makefile (ls | entr make refresh, which rebuilds and
then simulates a keypress in Arc/Firefox), but works in any browser
without AppleScript/xdotool or driving a specific app.
Build
Fetch dependencies with mor, then build:
make deps
make
make also runs make deps automatically. Dependencies are recorded in
requirements.ini and downloaded into thirdparty/, which
is ignored by git. TinyCThread is fetched this way and compiled alongside
liveside.c.
On Windows, make deps fetches TinyCThread plus win32ports headers for POSIX
interfaces such as dirent.h, getopt.h, sys/time.h, and unistd.h. The
Makefile detects $(OS)=Windows_NT, links Winsock, includes the native
ReadDirectoryChangesW watcher port, and produces liveside.exe.
Style
liveside adopts the suckless coding style
for C code. Use make format / make check-format with .astylerc; see
STYLE.md.
The served /liveside.js body is generated from client/liveside.js into
client/liveside.js.inc at build time and included by the C preprocessor, so
edit the JavaScript file directly rather than maintaining a C string copy.
Run
./liveside -d /path/to/site -p 8081
Options:
-d watch-dir— directory to watch recursively (default.)-p port— HTTP/SSE port (default8081)-c build-cmd— shell command to run (e.g.make all) before notifying clients; if it exits non-zero, clients are not told to reload-i poll-ms— polling interval in milliseconds, only used as a fallback on platforms without kqueue/inotify (default300)
Ctrl-C stops it.
License
BSD 3-Clause. See LICENSE.
Client
liveside serves its own client script, so pairing it with an existing
static server (e.g. darkhttpd on 8000 for the site, this on 8081 for
reload) needs nothing copied between them — just point an absolute
<script> tag at liveside's port:
<script src="http://localhost:8081/liveside.js"></script>
That's a cross-origin request, which script tags don't need CORS for. It
auto-detects the right origin to connect back to from its own src. If
you instead copy client/liveside.js into the site itself and load it
same-origin, set window.__RELOAD_PORT__ before the script tag so it
knows where to find the SSE stream.
How change detection works
- macOS / FreeBSD / NetBSD / OpenBSD / DragonFly BSD: opens a
kqueuewatch (EVFILT_VNODE) on every file and directory under the watched tree, so edits, adds, deletes, and renames are reported by the kernel the moment they happen — no polling delay. Because new files need their own watch, any event triggers a cheap rebuild of the watch set (a full re-walk), which also lets it notice freshly created files. A short debounce window coalesces the burst of events a single save often produces (write + truncate + attrib change) into one reload. - Linux: same idea via
inotify, but watches are per-directory (inotify reports changes to files inside a watched directory, unlike kqueue), so it needs far fewer watch descriptors for the same tree. - Windows: uses a native
ReadDirectoryChangesWwatcher over the recursive watch root, then filters events through the ignore rules before broadcasting reloads. - Everywhere else: falls back to polling — recursively hashing each file's
name, size, and mtime every
poll-msand comparing to the previous hash. Polling works fine, just with up topoll-msof latency instead of being instant.
Ignore rules are loaded from .gitignore and .livesideignore files at the
watch root and in nested directories. Parent rules apply before child rules;
within one directory, .gitignore is read before .livesideignore so
liveside-specific rules can override with !. Ignored paths are excluded from
polling snapshots and watcher scans, and native watcher events for ignored
paths are filtered before reloads are broadcast.
: nply/liveside