Skip to content

Instantly share code, notes, and snippets.

@rares
Created October 3, 2011 02:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rares/1258290 to your computer and use it in GitHub Desktop.
Save rares/1258290 to your computer and use it in GitHub Desktop.
require "sinatra"
require "set"
connections = Set.new
configure do
mime_type :event_stream, "text/event-stream"
set :server, :thin
end
get "/" do
erb :index
end
get "/stream" do
content_type :event_stream
status 200
stream true do |out|
connections << out
end
end
get "/message" do
erb :message
end
post "/message" do
connections.each do |out|
out << "data:#{params[:content]}\n\n"
end
redirect to(:message)
end
__END__
@@ index
<!DOCTYPE html>
<html>
<body>
<ul id="stream"></ul>
<script type="text/javascript">
(function() {
new EventSource("/stream").onmessage = function(e) {
document.getElementById("stream").innerHTML += "<li>" + e.data + "</li>";
};
})();
</script>
</body>
</html>
@@ message
<html>
<body>
<form method="post" action="/message">
<input type="text" name="content" />
<input type="submit" />
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment