Skip to content

Instantly share code, notes, and snippets.

@mcfiredrill
Last active December 15, 2015 04:19
Show Gist options
  • Save mcfiredrill/5200804 to your computer and use it in GitHub Desktop.
Save mcfiredrill/5200804 to your computer and use it in GitHub Desktop.
push metadata updates from liquidsoap to http stream in sinatra
* redis pub/sub
* no support for redis in liquidsoap, could call a ruby script?
* osc?
* supported directly in liquidsoap
* unix socket
* would require local sinatra app to communicate with the outside world
* write to file, poll filesystem with fs-events gem
* would require local sinatra app to communicate with the outside world
Redis idea seems good. I'm already a little familiar with the technology from working with Resque.
# adopted from https://github.com/gorsuch/sinatra-streaming-example/blob/master/worker.rb
require 'redis'
redis_url = ENV["REDISTOGO_URL"] || "redis://localhost:6379"
uri = URI.parse(redis_url)
r = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
meta = ARGV[0]
puts "setting metadata..."
r.publish "metadata", meta
source = on_metadata(pub_metadata, source)
def on_metadata(m) =
log("metadata changed: #{m}")
result = get_process_lines("./pub_metadata #{m}")
log("pub_metadata: #{result}")
end
<script>
var es = new EventSource('/metadata');
es.onmessage = function(e) { $('#streamtitle').html(e.title) };
</script>
# https://github.com/gorsuch/sinatra-streaming-example/blob/master/web.rb
require 'redis'
require 'sinatra'
configure do
redis_url = ENV["REDISTOGO_URL"] || "redis://localhost:6379"
uri = URI.parse(redis_url)
set :redis, Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
end
get '/metadata' do
puts "connection made"
stream do |out|
settings.redis.subscribe 'metadata' do |on|
on.message do |channel, message|
out << "#{message}\n"
end
end
end
end
@mcfiredrill
Copy link
Author

Seems to be working so far. But I can only stream the events to one client at a time, it seems like. It seems to be very flaky, sometimes the connection just hangs and I don't see any data for a long time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment