Commit d50c11d9 authored by Frank Rousseau's avatar Frank Rousseau
Browse files

Push tracker entry events to websocket channel

Generate events each time a tracker is created, modifided or deleted. Events
are broadcasted via websocket to clients (based on a Elixir channel).
parent a6599261
Pipeline #74486666 passed with stage
in 3 minutes and 42 seconds
defmodule Gaja.Endpoint do
use Phoenix.Endpoint, otp_app: :gaja
socket "/socket", Gaja.UserSocket,
socket "/socket", Gaja.Web.UserSocket,
websocket: true, # or list of options
longpoll: false
# Serve at "/" the static files from "priv/static" directory.
#
# You should set gzip to true if you are running phoenix.digest
# when deploying your static files in production.
plug Plug.Static,
at: "/", from: :gaja, gzip: false,
only: ~w(css fonts images js favicon.ico robots.txt)
# Code reloading can be explicitly enabled under the
# :code_reloader configuration of your endpoint.
if code_reloading? do
......@@ -39,6 +31,7 @@ defmodule Gaja.Endpoint do
signing_salt: "yRAUYsE8"
plug Gaja.Router
def init(_type, config) do
port = System.get_env("PORT") || 4000
host = System.get_env("HOST") || "localhost"
......
defmodule Gaja.Web.TrackersChannel do
use Gaja.Web, :channel
def join("trackers:room", payload, socket) do
if authorized?(payload) do
{:ok, socket}
else
{:error, %{reason: "unauthorized"}}
end
end
# Add authorization logic here as required.
defp authorized?(_payload) do
true
end
end
defmodule GajaWeb.TrackersChannelTest do
use Gaja.ChannelCase
setup do
{:ok, _, socket} =
socket(GajaWeb.UserSocket, "user_id", %{some: :assign})
|> subscribe_and_join(GajaWeb.TrackersChannel, "trackers:lobby")
{:ok, socket: socket}
end
"""
test "ping replies with status ok", %{socket: socket} do
ref = push socket, "ping", %{"hello" => "there"}
assert_reply ref, :ok, %{"hello" => "there"}
end
test "shout broadcasts to trackers:lobby", %{socket: socket} do
push socket, "shout", %{"hello" => "all"}
assert_broadcast "shout", %{"hello" => "all"}
end
test "broadcasts are pushed to the client", %{socket: socket} do
broadcast_from! socket, "broadcast", %{"some" => "data"}
assert_push "broadcast", %{"some" => "data"}
end
"""
end
defmodule Gaja.UserSocket do
defmodule Gaja.Web.UserSocket do
use Phoenix.Socket
## Channels
# channel "room:*", Gaja.RoomChannel
channel "trackers:*", Gaja.Web.TrackersChannel
## Transports
......
......@@ -31,6 +31,16 @@ defmodule Gaja.TrackerEntryController do
def create(%Plug.Conn{assigns: %{current_user: user}} = conn, params) do
tracker = Trackers.get_tracker(user, params["tracker_id"])
with {:ok, tracker_entry} <- TrackerEntries.create_tracker_entry(tracker, params) do
Gaja.Endpoint.broadcast(
"trackers:room",
"tracker-entry:new",
%{
id: tracker_entry.id,
tracker_id: tracker_entry.tracker_id,
date: tracker_entry.date,
value: tracker_entry.value
}
)
conn
|> put_status(201)
|> render("show.json", tracker_entry: tracker_entry)
......@@ -41,6 +51,16 @@ defmodule Gaja.TrackerEntryController do
tracker = Trackers.get_tracker(user, params["tracker_id"])
with tracker_entry = %TrackerEntry{} <- TrackerEntries.get_tracker_entry(tracker, params["id"]) do
changeset = TrackerEntries.update_tracker_entry(tracker_entry, params["tracker_entry"])
Gaja.Endpoint.broadcast(
"trackers:room",
"tracker-entry:update",
%{
id: tracker_entry.id,
tracker_id: tracker_entry.tracker_id,
date: tracker_entry.date,
value: tracker_entry.value
}
)
case changeset do
{:ok, tracker_entry} -> render(conn, "show.json", tracker_entry: tracker_entry)
{:error, _} -> changeset
......@@ -51,6 +71,16 @@ defmodule Gaja.TrackerEntryController do
def delete(%Plug.Conn{assigns: %{current_user: user}} = conn, params) do
tracker = Trackers.get_tracker(user, params["tracker_id"])
with tracker_entry = %TrackerEntry{} <- TrackerEntries.get_tracker_entry(tracker, params["id"]) do
Gaja.Endpoint.broadcast(
"trackers:room",
"tracker-entry:delete",
%{
id: tracker_entry.id,
tracker_id: tracker_entry.tracker_id,
date: tracker_entry.date,
value: tracker_entry.value
}
)
TrackerEntries.delete_tracker_entry(tracker_entry)
conn
|> put_status(204)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment