SSE server with Goliath server in HTML5
In this article I have describe about Goliath server.
What is HTML5 Server-Sent Events
- When a web page updates automatically from a server, it is a server-sent event.
- The web page would have to ask if any updates were available in server-sent events.
- The SSE servers can start data transmission for client, and send a message update.
- Facebook/Twitter updates, stock price updates, news feeds, sport results, etc.
SSE server with Goliath
- SSE server is simply use Goliath in this article and its make server implementa
- A WebSocket service we will likely need an entirely different backend.
- SSE endpoint can be implemented by any HTTP web server.
Example
require 'goliath'
class EventGenerator < Goliath::API
def response(env)
EM.add_periodic_timer(1) { env.stream_send("data:hello ##{rand(100)}\n\n") }
EM.add_periodic_timer(3) do
env.stream_send(["event:signup", "data:signup event ##{rand(100)}\n\n"].join("\n"))
end
streaming_response(200, {'Content-Type' => 'text/event-stream'})
end
end
class SSE < Goliath::API
use Rack::Static, :urls => ["/index.html"], :root => Goliath::Application.app_path("public")
get "/events" do
run EventGenerator.new
end
end
|
Define the code
- Goliath server to send request to event by returning a (Some no.) ok response.
- Then SSE Goliath starts emitting newline delimited message for each second.
- SSE Goliath is streaming directly into the HTTP connection.
- Goliath server is a static index file.
- SSE Goliath is open eventsource connection in HTML5.
Further Readings
You may also want to read these related articles :
Ask Your Question
Got a programming related question? You may want to post your question here
Programming Answers here