Last active
May 12, 2018 14:38
-
-
Save nouse/7008534 to your computer and use it in GitHub Desktop.
Roda Server Push Demo
This file contains hidden or 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 '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 |
This file contains hidden or 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 'typhoeus' | |
| request = Typhoeus::Request.new('http://localhost:5000/notifications') | |
| request.on_body do |chunk| | |
| puts chunk | |
| end | |
| request.run |
This file contains hidden or 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 './app' | |
| run App.freeze.app |
This file contains hidden or 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 'roda' | |
| gem 'typhoeus' | |
| gem 'thin' |
This file contains hidden or 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
| 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 |
This file contains hidden or 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
| 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