Permalink
Browse files

here's an example of SSE + hijack

with this, we could be able to build a more interactive site
  • Loading branch information...
godfat committed Sep 18, 2013
1 parent e04c29b commit 0bfb3bd470f70e20ca77d8cf3b47d1613c324a87
Showing with 16 additions and 0 deletions.
  1. +10 −0 lib/saya/api.rb
  2. +6 −0 public/index.html
@@ -7,6 +7,16 @@ module Saya
class API
include Jellyfish

get '/test' do
headers_merge('Content-Type' => 'text/event-stream',
'rack.hijack' => lambda do |io|
loop do
io.write("data: This is the first message.\n\n")
sleep 5
end
end)
end

twitter = %r{\A/?auth/twitter\Z}
post twitter do
rc_twitter{ |t|
@@ -11,6 +11,12 @@
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular-cookies.min.js"></script>
<script src="script.js"></script>
<script>
var source = new EventSource('/api/test');
source.onmessage = function (event) {
window.alert(event.data);
};
</script>
</head>

<body>

0 comments on commit 0bfb3bd

Please sign in to comment.