Created
August 13, 2012 02:15
-
-
Save amolpujari/3336469 to your computer and use it in GitHub Desktop.
app/channels/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'goliath' | |
require 'active_record' | |
require 'active_support' | |
#require 'em-synchrony' | |
#require 'em-synchrony/em-http' | |
# The location of the Rails app to integrate | |
RAILS_APP ||= "#{File.expand_path File.dirname(__FILE__)}/../.." | |
# Load the ActiveRecord database configuration, development settings | |
configpath = File.join(RAILS_APP, "config", "database.yml") | |
config = YAML::load_file(configpath) | |
ActiveRecord::Base.establish_connection config["development"] | |
require File.join(RAILS_APP, "app/channels", "post_channel.rb") | |
class SSEChannel < Goliath::API | |
# default to JSON output | |
#use Goliath::Rack::Render, 'json' | |
#use Goliath::Rack::Validation::RequestMethod, %w(GET) | |
def response(env) | |
#get '/posts' do | |
#run PostChannel.new | |
PostChannel.new | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require File.join(RAILS_APP, "app/models", "post.rb") | |
class PostChannel < Goliath::API | |
def response(env) | |
EM.add_periodic_timer(10) do | |
env.stream_send(Post.all.to_json) | |
end | |
streaming_response(200, {'Content-Type' => 'text/event-stream'}) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment