Skip to content

Instantly share code, notes, and snippets.

@jackbit
Forked from zpoley/em-stream-tail.rb
Created February 29, 2012 01:48
Show Gist options
  • Save jackbit/1936886 to your computer and use it in GitHub Desktop.
Save jackbit/1936886 to your computer and use it in GitHub Desktop.
Simple stream tail -f over http socket using Ruby EventMachine
#!/usr/bin/env ruby
#
# README: Start server with em-stream-tail.rb tail-filename
# Connect to server with curl http://localhost:8083
# Observe log events on curl.
#
require 'rubygems'
require 'eventmachine'
if ARGV.empty?
puts "usage: em-stream-tail.rb filename"
exit 0
end
class TailListener < EM::Connection
def self.spawn
EventMachine.popen("tail -f #{ARGV[0]}", TailListener)
end
def receive_data data
TailServer.announce data
end
end
class TailServer < EM::Connection
@@connected_clients = []
def self.announce data
@@connected_clients.each { |cc| cc.send_data data }
end
def initialize
@@connected_clients << self
end
def receive_data data; end
def unbind
@@connected_clients.delete self
end
end
EventMachine.run do
TailListener.spawn
EventMachine.start_server("0.0.0.0", 8083, TailServer)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment