Skip to content

Instantly share code, notes, and snippets.

@mhuneke
Created October 18, 2012 15:10
Show Gist options
  • Save mhuneke/3912458 to your computer and use it in GitHub Desktop.
Save mhuneke/3912458 to your computer and use it in GitHub Desktop.
Live Reload client-server
// <script src="node_modules/socket.io/node_modules/socket.io-client/dist/socket.io.js"></script>
var socket = io.connect();
socket.on('server:reload', function(message) {
document.location.reload();
});
var io = require('socket.io').listen(app);
var fs = require('fs');
function watchDirRecursive(directory, extensions, fn) {
fs.readdir(directory, function(err, files) {
if (err) throw err;
for (var i = 0; i < files.length; i++) {
var file = directory + '/' + files[i];
var stat = fs.statSync(file);
if (stat.isDirectory() && files[i][0] != '.') {
watchDirRecursive(file, extensions, fn);
} else if (stat.isFile()) {
var extIdx = file.lastIndexOf('.');
if (extIdx > 0 && extensions.indexOf(file.substr(extIdx+1)) > -1) {
fs.watchFile(file, fn);
}
}
}
});
}
watchDirRecursive('.', ['html', 'css', 'js'], function() {
io.sockets.emit('server:reload');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment