Crowdhailer

Crowdhailer

Creator of Raxx

ServerSentEvent library for parsing and consuming as a Client

Push updates to web clients over HTTP, using dedicated server-push protocol.

https://github.com/CrowdHailer/server_sent_event.ex/

Server Sent Events are a way to push updates to web clients, including browsers. See the spec for more details.

This project includes the tools for manipulating, serializing and parsing Events in this format.
It can be used to build both clients and servers.

Event Client

This library now also includes an Elixir Client for an event stream.
The client behaviour defines callbacks that handle connection/disconnect/reconnect and incoming events.

A simple client that always connects could be defined as follows

defmodule AutoConnect do
  @behaviour ServerSentEvent.Client

  # Start connecting to the endpoint as soon as client is started.
  def init(state) do
    {:connect, request(state), state}
  end

  # The client has successfully connected, or reconnected, to the event stream.
  def handle_connect(_response, state) do
    {:noreply, state}
  end

  # Retry connecting to endpoint 1 second after a failure to connect.
  def handle_connect_failure(reason, state) do
    Process.sleep(1_000)
    {:connect, request(state), state}
  end

  # Immediatly try to reconnect when the connection is lost.
  def handle_disconnect(_, state) do
    {:connect, request(state), state}
  end

  # Update the running state of the client with the id of each event as it arrives.
  # This event id is used for reconnection.
  def handle_event(event, state) do
    IO.puts("I just got a new event: #{inspect(event)}")
    %{state | last_event_id: event.id}
  end

  # When stop message is received this process will exit with reason :normal.
  def handle_info(:stop, state) do
    {:stop, :normal, state}
  end

  # Not a callback but helpful pattern for creating requests in several callbacks
  defp request({url: url}) do
    Raxx.request(:GET, url)
    |> Raxx.set_header("accept", "text/event-stream")
    |> Raxx.set_header("last-event-id", state.last_event_id)
  end
end

# Starting the client
{:ok, pid} = AutoConnect.start_link(%{url: "http://www.example.com/events", last_event_id: "0"})

The client can also be added to a supervision tree

children = [
  {AutoConnect, %{url: "http://www.example.com/events", last_event_id: "0"}}
]

Most Liked

Crowdhailer

Crowdhailer

Creator of Raxx

0.4.7 Make parsing 2_000x faster.

Just that.

See issue for benchmark and discussions.
https://github.com/CrowdHailer/server_sent_event.ex/pull/11

Crowdhailer

Crowdhailer

Creator of Raxx

1.0.0 released

Removes support for pre-stable versions of Raxx when using the ServerSentEvent.Client.

WolfDan

WolfDan

Is just me, or maybe you forgot to link the project @Crowdhailer ? ^^

Where Next?

Popular in Announcing Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29603 241
New
josevalim
Hi everyone, We would like to announce that Plataformatec is working on a new MySQL driver called MyXQL. Our goal is to eventually integ...
New
dominicletz
Hi, I thought I had posted my library before but seems I hadn’t. The project is still in early stages but it’s growing and so I think it...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36352 110
New
versilov
Could not wait for the missing Elixir ML libraries to appear, so, I wrote one myself, taking https://github.com/sdwolfz/exlearn as a foun...
New
hpopp
After just over two years in development, this latest version of Pigeon is what I finally consider done in regards to my original vision ...
New
zoltanszogyenyi
Hey everyone :waving_hand: Excited to join this forum - I am one of the founders and current project maintainers of a popular and open-s...
New
mplatts
With HEEX released we decided to start a components library using Tailwind CSS - check it out here: Petal Components. We also have a boi...
New
wfgilman
I’ve cleaned up and open sourced three financial libraries I was using for my company. They are bindings for the APIs of these three comp...
New
mattludwigs
Grizzly is a library for working with Z-Wave devices. Z-Wave is a low-frequency radio protocol for controlling smart home devices on a me...
New

Other popular topics Top

WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36352 110
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54120 245
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement