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.
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
Creator of Raxx
Crowdhailer
Creator of Raxx
WolfDan
Is just me, or maybe you forgot to link the project @Crowdhailer ? ^^
1
joefractal
Now I get it, thanks
1
Popular in Libraries
Hi! Today, after a couple weeks of development I’ve released v0.1 of LiveVue.
It’s a seamless integration of Vue and Phoenix LiveView, i...
New
I’ve just released stable versions of my Prometheus Elixir libs:
Elixir client [docs];
Ecto collector [docs];
Plugs instrumenter/Export...
New
Introducing assertions, the library that helps you write really great test assertions!
GitHub: https://github.com/devonestes/assertions ...
New
I’ve just released version 3 of Comeonin, a password hashing library.
The following small changes have been made:
changes to the NIF c...
New
Hey there!
I wrote a download elixir package which does exactly what its name about - an easy way to download files.
I saw solutions ...
New
Today I released a new dialyzer Mix task as the dialyzex package! At the time we started writing this task, the existing dialyzer integra...
New
I released Doggo, a collection of unstyled Phoenix components.
Features
Unstyled Phoenix components.
Storybook that can be added to...
New
It is a well-know topic within the Elixir community: “To mock or not to mock? :)”
Every alchemist probably has his / her own opinion con...
New
PhoenixWS - Websockets over Phoenix Channels
Source code on Github here: https://github.com/tmbb/phoenix_ws
Phoenix channels are a great...
New
Hi, I wanted share a small library we at Revelry Labs made for rendering react components from the server side. There are instructions fo...
New
Other popular topics
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
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
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
Good day to you all.
I have been struggling to get a query involving like and ilike to work.
Can anyone assist me on this, please?
pro...
New
I have a User schema with a :from_id field set to type :string:
defmodule TweetBot.Repo.Migrations.CreateUsers do
use Ecto.Migration
...
New
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New







