The best way to integrate the runner in your workflow is to add it as a reload
step within your build tool. This build tool can then use the internal binary
linked by npm in node_modules/.bin/tiny-lr to not rely on global installs (or
use the server programmtically).

You can start the server using the binary provided, or use your own start script.

var tinylr =require('tiny-lr');

// standard LiveReload port

var port =35729;

// tinylr(opts) => new tinylr.Server(opts);

tinylr().listen(port,function(){

if(err){

// deal with err

return;

}

console.log('... Listening on %s (pid: %s) ...', port);

})

You can define your own route and listen for specific request:

var server =tinylr();

server.on('GET /myplace',function(req,res){

res.write('Mine');

res.end();

})

And stop the server manually:

server.close();

This will close any websocket connection established and emit a close event.

Middleware

To use as a connect / express middleware, tiny-lr needs query /
bodyParse middlewares prior in the stack.

Any handled requests ends at the tinylr level, not found and errors are
nexted to the rest of the stack.

// This binds both express app and tinylr on the same port

var app =express();

app.use(express.query())

.use(express.bodyParser())

.use(tinylr.middleware({ app: app }))

.use(express.static(path.resolve('./')))

.use(express.directory(path.resolve('./')))

.listen(35729,function(){

console.log('Listening on %d',35729);

})

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. Check the
examples/express/server.js file to see how.

Using grunt

This package exposes a tasks/ directory, that you can use within your Gruntfile with:

grunt.loadNpmTasks('tiny-lr');

tinylr-start - Starts a new tiny-lr Server, with the provided port.

tinylr-reload - Sends a reload notification to the previously started server.

tinylr-start should be used with the watch task, probably with an alias
that triggers both tinylr-start watch tasks.

tinylr-reload should be configured as a "watch" task in your Gruntfile.

grunt.initConfig({

watch:{

reload:{

files:['**/*.html','**/*.js','**/*.css','**/*.{png,jpg}'],

tasks:'tinylr-reload'

}

}

});

grunt.registerTask('reload',['tinylr-start','watch']);

Using make

See tasks/tiny-lr.mk.

Include this file into your project Makefile to bring in the following targets:

start - Start the LiveReload server

stop - Stops the LiveReload server

livereload - alias to start

livereload-stop - aias to stop

Then define your "empty" targets, and the list of files you want to monitor.

CSS_DIR = app/styles

CSS_FILES = $(shell find $(CSS_DIR) -name '*.css')

# include the livereload targets

include node_modules/tiny-lr/tasks/*.mk

$(CSS_DIR): $(CSS_FILES)

@echo CSS files changed: $?

@touch $@

curl -X POST http://localhost:35729/changed -d '{ "files": "$?" }'

reload-css: livereload $(CSS_DIR)

.PHONY: reload-css

The pattern is always the same:

define a target for your root directory that triggers a POST request

touch the directory to update its mtime

add reload target with livereload and the list of files to "watch" as
prerequisites