diff --git a/morse/lib/morse/server.ex b/morse/lib/morse/server.ex index 10b768e..a76dae0 100644 --- a/morse/lib/morse/server.ex +++ b/morse/lib/morse/server.ex @@ -37,6 +37,6 @@ defmodule Morse.Server do end defp broadcast_progress(progress) do - UiWeb.Endpoint.broadcast("morse:progress", "update", %{value: progress}) + GenServer.cast(Ui.SocketAPI, {:broadcast_progress, progress}) end end diff --git a/ui/lib/ui/application.ex b/ui/lib/ui/application.ex index 2d39b6c..7bdd4f4 100644 --- a/ui/lib/ui/application.ex +++ b/ui/lib/ui/application.ex @@ -9,7 +9,8 @@ defmodule Ui.Application do # List all child processes to be supervised children = [ # Start the endpoint when the application starts - UiWeb.Endpoint + UiWeb.Endpoint, + Ui.SocketAPI # Starts a worker by calling: Ui.Worker.start_link(arg) # {Ui.Worker, arg}, ] diff --git a/ui/lib/ui/socket_api.ex b/ui/lib/ui/socket_api.ex new file mode 100644 index 0000000..20775e0 --- /dev/null +++ b/ui/lib/ui/socket_api.ex @@ -0,0 +1,18 @@ +defmodule Ui.SocketAPI do + use GenServer + + def start_link(_) do + GenServer.start_link(__MODULE__, nil, name: __MODULE__) + end + + @impl true + def init(state) do + {:ok, state} + end + + @impl true + def handle_cast({:broadcast_progress, progress}, state) do + UiWeb.Endpoint.broadcast("morse:progress", "update", %{value: progress}) + {:noreply, state} + end +end