Skip to content

Commit

Permalink
Update live stream card in real-time
Browse files Browse the repository at this point in the history
  • Loading branch information
ku1ik committed Apr 21, 2024
1 parent 4412034 commit c3e857e
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 10 deletions.
23 changes: 16 additions & 7 deletions lib/asciinema/streaming/live_stream_server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ defmodule Asciinema.Streaming.LiveStreamServer do
GenServer.call(via_tuple(stream_id), :heartbeat)
end

def subscribe(stream_id, type) when type in [:reset, :feed, :offline] do
def subscribe(stream_id, type) when type in [:reset, :feed, :offline, :metadata] do
PubSub.subscribe(topic_name(stream_id, type))
end

Expand Down Expand Up @@ -165,12 +165,21 @@ defmodule Asciinema.Streaming.LiveStreamServer do
stream =
case state.vt_size do
{cols, rows} ->
Streaming.update_live_stream(state.stream,
current_viewer_count: state.viewer_count,
cols: cols,
rows: rows,
snapshot: generate_snapshot(state.vt)
)
stream =
Streaming.update_live_stream(state.stream,
current_viewer_count: state.viewer_count,
cols: cols,
rows: rows,
snapshot: generate_snapshot(state.vt)
)

publish(state.stream_id, %Update{
stream_id: state.stream_id,
event: :metadata,
data: stream
})

stream

nil ->
state.stream
Expand Down
1 change: 0 additions & 1 deletion lib/asciinema_web/controllers/live_stream/card.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

<small>
by <.link href={author_profile_path(@stream)}><%= author_username(@stream) %></.link>
<%= time_ago_tag(@stream.last_started_at) %>
</small>
</div>
</div>
4 changes: 3 additions & 1 deletion lib/asciinema_web/controllers/user/show.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@
<div class="col-md-12">
<div :for={streams <- Enum.chunk_every(@streams, 2, 2)} class="row">
<div :for={stream <- streams} class="col-md-6">
<LiveStreamHTML.card stream={stream} />
<%= live_render(@conn, AsciinemaWeb.LiveStreamCardLive,
session: %{"stream_id" => stream.id}
) %>
</div>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion lib/asciinema_web/controllers/user_html.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule AsciinemaWeb.UserHTML do
import AsciinemaWeb.ErrorHelpers
import Scrivener.HTML
alias Asciinema.{Fonts, Gravatar}
alias AsciinemaWeb.{LiveStreamHTML, MediaView, RecordingHTML}
alias AsciinemaWeb.{MediaView, RecordingHTML}

embed_templates "user/*"

Expand Down
28 changes: 28 additions & 0 deletions lib/asciinema_web/live/live_stream_card_live.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
defmodule AsciinemaWeb.LiveStreamCardLive do
use AsciinemaWeb, :live_view
alias Asciinema.Streaming
alias Asciinema.Streaming.LiveStreamServer

@impl true
def render(assigns) do
~H"""
<AsciinemaWeb.LiveStreamHTML.card stream={@stream} />
"""
end

@impl true
def mount(_params, %{"stream_id" => stream_id}, socket) do
socket = assign(socket, :stream, Streaming.get_live_stream(stream_id))

if connected?(socket) do
LiveStreamServer.subscribe(stream_id, :metadata)
end

{:ok, socket}
end

@impl true
def handle_info(%LiveStreamServer.Update{event: :metadata} = update, socket) do
{:noreply, assign(socket, stream: update.data)}
end
end

0 comments on commit c3e857e

Please sign in to comment.