Skip to content

Instantly share code, notes, and snippets.

@svenyurgensson
Forked from pirj/Gemfile
Last active December 12, 2015 02:38
Show Gist options
  • Save svenyurgensson/4700462 to your computer and use it in GitHub Desktop.
Save svenyurgensson/4700462 to your computer and use it in GitHub Desktop.
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
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'
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