Blog – #elixir

Implementing Long-Running HTTP Connections in Phoenix (override Cowboy's idle_timeout)

The Elixir language's actor programming model and the Phoenix web framework are particularly well-suited to implement long-running streaming HTTP connections, such as used in Server-Sent Events (SSE). For example, this blog post on Server-Sent Events with Elixir by Krister Viirsaar succinctly demonstrates how an SSE endpoint can be implemented in Phoenix without using any external libraries. However, the Cowboy HTTP server terminates idle connections after a globally-configured idle timeout, which is only reset when new data is received by clients. Also, SSE streams should send keep-alive messages regularly to ensure that clients, reverse-proxies, and middle-boxes don't close such connections. In this post, I will extend a minimal Phoenix SSE request endpoint example by adding keep-alive messages and overriding the Cowboy HTTP server idle timeout.