Skip to content

Instantly share code, notes, and snippets.

@jgoulah
Created March 14, 2010 21:49
Show Gist options
  • Select an option

  • Save jgoulah/332259 to your computer and use it in GitHub Desktop.

Select an option

Save jgoulah/332259 to your computer and use it in GitHub Desktop.
// require the node.js system and filesystem modules
var sys = require('sys'),
fs = require('fs');
var Module = this.Module = function(data, connection){
// connection is the instance of websocket::Connection
};
Module.prototype.onData = function(data, connection) {
// client sends 'start' when the socket first connects
if (data == 'start'){
var monitor_file = function(filename) {
var cmd = process.createChildProcess("tail", ["-f", filename]);
cmd.addListener("output", function(data) {
// break up JSON objects and iterate over them
var lines = data.split("\r");
for (var entry in lines) {
try {
// turn the json into an object
var pdata = eval('(' + lines[entry] + ')');
// output to terminal
sys.puts(pdata['text']);
// send it over the socket
connection.send(pdata['user']['screen_name'] +': ' + pdata['text']);
sys.puts('');
} catch (err) {}
}
});
};
monitor_file("/home/jgoulah/sample.json");
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment