vorce / sse

Simple demo of Server Sent Events using Cowboy and Plug in Elixir

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Sse

A simple demo of Server-sent Events (SSE) using Cowboy and Plug in Elixir.

Just a learning thing. Much help from these gists: jeregrin, rbishop

Server sent events

TL;DR is that it's a simple technology for streaming data/events to a client over a long running HTTP connection.

Server-Sent Events Specification on w3

Stuff to explore further

  • retry: <milliseconds>: can be used to configure the client's reconnection time(?)
  • id: <id>: before a data: line associates the data with the id, clients may keep track of that id and in case of a reconnection can add a header Last-Event-ID which then could be used by the server to re-send everything that was missed.
  • event: <name>: can be used to associate the next data: line with a category/type of event.
  • To make sure the connection is not closed by for example proxies, firewalls etc I could send heartbeats. (For example just a line starting with :, since those should be ignored by the client)

Running

Make sure you have Elixir installed.

  1. mix deps.get
  2. mix run --no-halt
  3. The demo app should now be running and listening on localhost:4000. On /events you can get an infinite stream of events. Ex: curl localhost:4000/events

About

Simple demo of Server Sent Events using Cowboy and Plug in Elixir


Languages

Language:Elixir 100.0%