tiny-lr
This script manages a tiny LiveReload server implementation.
It exposes an HTTP server and express middleware, with a very basic REST Api to notify the server of a particular change.
It doesn't have any watch ability, it must be done at the build process or application level.
Instead, it exposes a very simple API to notify the server that some changes have been made, then broadcasted to every livereload client connected.
# notify a single change
curl http://localhost:35729/changed?files=style.css
# notify using a longer path
curl http://localhost:35729/changed?files=js/app.js
# notify multiple changes, comma or space delimited
curl http://localhost:35729/changed?files=index.html,style.css,docs/docco.css
Or you can bulk the information into a POST request, with body as a JSON array of files.
curl -X POST http://localhost:35729/changed -d '{ "files": ["style.css", "app.js"] }'
# from a JSON file
node -pe 'JSON.stringify({ files: ["some.css", "files.css"] })' > files.json
curl -X POST -d @files.json http://localhost:35729
As for the livereload client, you need to install the browser extension: http://feedback.livereload.com/knowledgebase/articles/86242-how-do-i-install-and-use-the-browser-extensions- (note: you need to listen on port 35729 to be able to use with your brower extension)
or add the livereload script tag manually: http://feedback.livereload.com/knowledgebase/articles/86180-how-do-i-add-the-script-tag-manually- (and here you can choose whatever port you want)
Integration
The best way to integrate the runner in your workflow is to add it as a reload
step within your build tool.
var tinylr = ; // standard LiveReload portvar port = 35729; // tinylr(opts) => new tinylr.Server(opts);
You can define your own route and listen for specific request:
var server = ; server
And stop the server manually:
server;
This will close any websocket connection established and emit a close event.
Middleware
To use as a connect / express middleware, tiny-lr needs query / bodyParser middlewares prior in the stack (to handle POST requests)
Any handled requests ends at the tinylr level, not found and errors are nexted to the rest of the stack.
var port = processenvLR_PORT || processenvPORT || 35729; var path = ;var express = ;var tinylr = ;var body = ; var app = ; // This binds both express app and tinylr on the same port app ;
The port you listen on is important, and tinylr should always listen on
the LiveReload standard one: 35729
. Otherwise, you won't be able to rely
on the browser extensions, though you can still use the manual snippet
approach.
You can also start two different servers, one on your app port, the other listening on the LiveReload port.
Using grunt
Head over to https://github.com/gruntjs/grunt-contrib-watch
Using make
See make-livereload repo. This repository defines a bin wrapper you can use and install with:
npm install make-livereload -g
It bundles the same bin wrapper previously used in tiny-lr repo.
Usage: tiny-lr [options]
Options:
-h, --help output usage information
-V, --version output the version number
port -p
pid Path to the generated PID file (default: ./tiny-lr.pid)
Using gulp
See gulp-livereload repo.
Tests
npm test
TOC # tiny-lr accepts ws clients.
var url = ;var server = thisapp; var ws = thisws = 'ws://' + urlhost + '/livereload'; ws { var hello = command: 'hello' protocols: 'http://livereload.com/protocols/official-7' ; ws;}; ws { assert; assert; ;};
properly cleans up established connection on exit.
var ws = thisws; wsonclose = done; ;
;
unknown route respond with proper 404 and error message.
;
;
with no clients, some files.
;
;
with no clients, some files.
var data = clients: files: 'cat.css' 'sed.css' 'ack.js' ; ;
;
var server = thisserver; ;
Thanks!
-
Tiny-lr is a LiveReload implementation. They really made frontend editing better for a lot of us. They have a LiveReload App on the Mac App Store you might want to check out.
-
To all contributors
-
@FGRibreau / pid.js gist) for the background friendly bin wrapper, used in make-livereload