diff --git a/assets/js/app.js b/assets/js/app.js
index ee7f86f..476cdc1 100644
--- a/assets/js/app.js
+++ b/assets/js/app.js
@@ -181,16 +181,16 @@ Hooks.AudioPlayer = {
Hooks.Ping = {
mounted(){
this.handleEvent("pong", () => {
- console.log("pong")
- this.el.innerText = `ping: ${Date.now() - this.nowMs}ms`
- this.timer = setTimeout(() => this.ping(), 1000)
+ let rtt = Date.now() - this.nowMs
+ this.el.innerText = `ping: ${rtt}ms`
+ this.timer = setTimeout(() => this.ping(rtt), 1000)
})
- this.ping()
+ this.ping(null)
},
destroyed(){ clearTimeout(this.timer) },
- ping(){
+ ping(rtt){
this.nowMs = Date.now()
- this.pushEvent("ping", {})
+ this.pushEvent("ping", {rtt: rtt})
}
}
diff --git a/lib/live_beats/media_library.ex b/lib/live_beats/media_library.ex
index bbee71e..7335ecd 100644
--- a/lib/live_beats/media_library.ex
+++ b/lib/live_beats/media_library.ex
@@ -30,6 +30,10 @@ defmodule LiveBeats.MediaLibrary do
Phoenix.PubSub.subscribe(@pubsub, topic(profile.user_id))
end
+ def broadcast_ping(%Accounts.User{} = user, rtt, region) do
+ broadcast!(user.active_profile_user_id, {:ping, %{rtt: rtt, region: region}})
+ end
+
def unsubscribe_to_profile(%Profile{} = profile) do
Phoenix.PubSub.unsubscribe(@pubsub, topic(profile.user_id))
end
@@ -251,7 +255,9 @@ defmodule LiveBeats.MediaLibrary do
end
def get_current_active_song(%Profile{user_id: user_id}) do
- Repo.replica().one(from s in Song, where: s.user_id == ^user_id and s.status in [:playing, :paused])
+ Repo.replica().one(
+ from s in Song, where: s.user_id == ^user_id and s.status in [:playing, :paused]
+ )
end
def get_profile!(%Accounts.User{} = user) do
diff --git a/lib/live_beats_web/live/nav.ex b/lib/live_beats_web/live/nav.ex
index af1da2b..af4085d 100644
--- a/lib/live_beats_web/live/nav.ex
+++ b/lib/live_beats_web/live/nav.ex
@@ -31,7 +31,13 @@ defmodule LiveBeatsWeb.Nav do
{:cont, assign(socket, active_tab: active_tab)}
end
- defp handle_event("ping", _, socket) do
+ defp handle_event("ping", %{"rtt" => rtt}, socket) do
+ %{current_user: current_user} = socket.assigns
+
+ if rtt && current_user && current_user.active_profile_user_id do
+ MediaLibrary.broadcast_ping(current_user, rtt, socket.assigns.region)
+ end
+
{:halt, push_event(socket, "pong", %{})}
end
diff --git a/lib/live_beats_web/live/profile_live.ex b/lib/live_beats_web/live/profile_live.ex
index f56a290..d224fbf 100644
--- a/lib/live_beats_web/live/profile_live.ex
+++ b/lib/live_beats_web/live/profile_live.ex
@@ -40,7 +40,13 @@ defmodule LiveBeatsWeb.ProfileLive do