Skip to content

Instantly share code, notes, and snippets.

@1gor
Forked from nouse/Gemfile
Created May 12, 2018 14:38
Show Gist options
  • Save 1gor/553d3f8e5b44c805ff85192d1bfcdaa2 to your computer and use it in GitHub Desktop.
Save 1gor/553d3f8e5b44c805ff85192d1bfcdaa2 to your computer and use it in GitHub Desktop.
Roda Server Push Demo
require 'roda'
class App < Roda
plugin :streaming
route do |r|
r.root do
<<-END
<!doctype html>
<html>
<head>
<meta charset="utf8"/>
<title> Server Push Example</title>
</head>
<body>
Server Push Example
<script type="text/javascript">
var es = new EventSource('/notifications');
es.addEventListener('message', function(e) {
console.log(e.data);
});
es.addEventListener('error', function(e) { es.close(); });
</script>
</body>
</html>
END
end
r.get 'notifications' do
response['Content-Type'] = 'text/event-stream'
stream do |out|
(1..10).each do |i|
sleep 1
out << "id: #{i}\n"
out << "data: return #{i*i} \n\n"
end
end
end
end
end
require 'typhoeus'
request = Typhoeus::Request.new('http://localhost:5000/notifications')
request.on_body do |chunk|
puts chunk
end
request.run
require './app'
run App.freeze.app
source 'https://rubygems.org'
gem 'roda'
gem 'typhoeus'
gem 'thin'
GEM
remote: https://rubygems.org/
specs:
daemons (1.2.2)
ethon (0.7.3)
ffi (>= 1.3.0)
eventmachine (1.0.7)
ffi (1.9.8)
rack (1.6.1)
roda (2.3.0)
rack
thin (1.6.3)
daemons (~> 1.0, >= 1.0.9)
eventmachine (~> 1.0)
rack (~> 1.0)
typhoeus (0.7.1)
ethon (>= 0.7.1)
PLATFORMS
ruby
DEPENDENCIES
roda
thin
typhoeus
web: bundle exec thin start -p $PORT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment