Crowdhailer

Crowdhailer

Creator of Raxx

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"}}
]

Showing Posts 1 to 9

WolfDan

WolfDan

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

Crowdhailer

Crowdhailer OP

Creator of Raxx

Fixed, thanks

joefractal

joefractal

Is there any javascript to go with this, so it could be used from a browser?

Crowdhailer

Crowdhailer OP

Creator of Raxx

There is already a browser API for consuming ServerSentEvents.

The fact this API exists is the main reason I use them.

joefractal

joefractal

Now I get it, thanks

Crowdhailer

Crowdhailer OP

Creator of Raxx

0.4.3 Fixes connection edgecases in the ServerSentEvent.Client

See CHANGELOG for details

Crowdhailer

Crowdhailer OP

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 OP

Creator of Raxx

0.4.8 Add support for raxx 0.18.0

No breaking changes in this version of raxx affect this library so it now supports 0.16.x, 0.17.x and 0.18.x versions of raxx

Crowdhailer

Crowdhailer OP

Creator of Raxx

1.0.0 released

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

— All posts loaded —

Where Next? Top

Trending in Announcing Top

woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
New
MRdotB
I needed to reuse React components from my Chrome extension in my Phoenix/LiveView backend. I noticed that for Svelte/Vue, there are live...
New
woylie
I released Doggo, a collection of unstyled Phoenix components. https://github.com/woylie/doggo Features Unstyled Phoenix components....
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
anuaralfetahe
Hello Published a new library - ProcessHub! ProcessHub is a library designed to manage process distribution within the Elixir cluster. ...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New
sergio
It’s not that it’s vocabulary is too advanced. It’s something worse. I get lost trying to follow even a paragraph written by Claude. It’...
New
AstonJ
This showed up on my feed.. anyone heard of it? Just hype? Ox Alpha is a reasoning model designed for coding, sustained ag...
New
bartblast
Hey folks, I just published a post about Hologram’s funding and where the project goes next - the short version: Curiosum as Main Spons...
New
sorenone
Today we’re releasing Oban for Python. Not an Oban client in Python. Not a pythonx wrapper embedded in Elixir. Nope, it’s a fully operati...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews