this repo has no description

Add websocket API for reading stories

hauleth.dev e1536ff7 b2bc217a

verified
Changed files
+46
lib
esl_hn_web
+44
lib/esl_hn_web/api/socket.ex
···
+
defmodule EslHnWeb.API.Socket do
+
@behaviour Phoenix.Socket.Transport
+
+
@impl true
+
def child_spec(_opts), do: :ignore
+
+
@impl true
+
def connect(_connect_info) do
+
{:ok, []}
+
end
+
+
@impl true
+
def init(state) do
+
# No support for `handle_continue/2` so we need to hack it around
+
send(self(), :do_init)
+
EslHn.subscribe_new()
+
+
{:ok, state}
+
end
+
+
@impl true
+
def handle_in({message, opts}, state) do
+
dbg({message, opts})
+
+
{:ok, state}
+
end
+
+
@impl true
+
def handle_info(:do_init, state) do
+
stories = EslHn.all(1, 50)
+
data = EslHnWeb.API.JSON.index(%{items: stories})
+
+
{:push, {:text, JSON.encode_to_iodata!(data)}, state}
+
end
+
+
def handle_info({:new_stories, stories}, state) do
+
data = EslHnWeb.API.JSON.index(%{items: stories})
+
+
{:push, {:text, JSON.encode_to_iodata!(data)}, state}
+
end
+
+
@impl true
+
def terminate(_reason, _state), do: :ok
+
end
+2
lib/esl_hn_web/endpoint.ex
···
websocket: [connect_info: [session: @session_options]],
longpoll: [connect_info: [session: @session_options]]
+
socket "/", EslHnWeb.API.Socket
+
plug Plug.Static,
at: "/",
from: :esl_hn,