-
-
Save svenyurgensson/4700462 to your computer and use it in GitHub Desktop.
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 'bundler' | |
Bundler.require | |
class App < Sinatra::Base | |
helpers Sinatra::Streaming | |
get '/' do | |
subscriber = EM::Hiredis.connect | |
subscriber.psubscribe '*' | |
stream :keep_open do |out| | |
subscriber.on(:pmessage) do |key, channel, message| | |
subscriber.punsubscribe '*' if out.closed? | |
out << "#{key}, #{channel}, #{message}" unless out.closed? | |
end | |
end | |
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
source 'https://rubygems.org' | |
gem 'sinatra' | |
gem 'sinatra-contrib', require: 'sinatra/streaming' | |
group :development do | |
gem 'thin' | |
gem 'pry-rails' | |
end | |
gem 'eventmachine', "= 1.0.0.rc.4" | |
gem "redis", "~> 3.0" | |
gem "hiredis", "~> 0.4.5" | |
gem 'em-hiredis' | |
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
jQuery doesn't support that, but you can do that with plain XHR: | |
var xhr = new XMLHttpRequest() | |
xhr.open("GET", "/test/chunked", true) | |
xhr.onprogress = function () { | |
alert("PROGRESS: " + xhr.responseText) | |
} | |
This works in all modern browsers, including IE 10. W3C specification here. | |
The downside here is that xhr.responseText contains an accumulated response. You can use substring on it, but a better idea is to use the responseType attribute and use slice on an ArrayBuffer. | |
http://stackoverflow.com/questions/6789703/how-to-write-javascript-in-client-side-to-receive-and-parse-chunked-response-i/12280916#12280916 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment