Skip to content

Instantly share code, notes, and snippets.

@kfigiela
Last active August 29, 2015 14:09
Show Gist options
  • Save kfigiela/ea6d82089f375c375aea to your computer and use it in GitHub Desktop.
Save kfigiela/ea6d82089f375c375aea to your computer and use it in GitHub Desktop.
node_modules

tail -f on websockets

Usage

  • npm install
  • node ./index.js
<!doctype html>
<html>
<head>
<title>Socket.IO chat</title>
<style>
body, html { padding:0; margin: 0; font-family: monospace;}
#messages { list-style-type: none; margin: 0; padding: 0; }
#messages li { padding: 5px 10px; }
#messages li:nth-child(odd) { background: #eee; }
</style>
</head>
<body>
<ul id="messages"></ul>
<script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
<script src="http://code.jquery.com/jquery-1.11.1.js"></script>
<script>
var socket = io(":3000");
socket.on('log', function(msg){
$('#messages').append($('<li>').text(msg));
});
</script>
</body>
</html>
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var spawn = require('child_process').spawn;
var tail = spawn("tail", ["-f", "/var/log/system.log"])
app.get('/', function(req, res){
res.sendfile(__dirname + '/index.html');
});
tail.stdout.on("data", function (data) {
io.emit('log', data.toString('utf-8'));
});
io.on('connection', function(socket){
console.log("Client connected")
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
{
"name": "socket-chat-example",
"version": "0.0.1",
"description": "my first socket.io app",
"dependencies": {
"express": "4.3.1",
"socket.io": "1.2.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment