diff --git a/lib/livebook_web/channels/js_view_channel.ex b/lib/livebook_web/channels/js_view_channel.ex index 77d095881..13ca1a992 100644 --- a/lib/livebook_web/channels/js_view_channel.ex +++ b/lib/livebook_web/channels/js_view_channel.ex @@ -82,6 +82,15 @@ defmodule LivebookWeb.JSViewChannel do {:noreply, socket} end + def handle_info({:event, event, payload, %{ref: ref}}, socket) do + with {:error, error} <- try_push(socket, "event:#{ref}", [event], payload) do + message = "Failed to serialize widget data, " <> error + push(socket, "error:#{ref}", %{"message" => message}) + end + + {:noreply, socket} + end + def handle_info({:pong, _, %{ref: ref}}, socket) do push(socket, "pong:#{ref}", %{}) {:noreply, socket} diff --git a/test/livebook_web/channels/js_view_channel_test.exs b/test/livebook_web/channels/js_view_channel_test.exs index 718a7c57c..7dbf4fc86 100644 --- a/test/livebook_web/channels/js_view_channel_test.exs +++ b/test/livebook_web/channels/js_view_channel_test.exs @@ -47,6 +47,16 @@ defmodule LivebookWeb.JSViewChannelTest do assert_receive {:event, "ping", [1, 2, 3], %{origin: _origin}} end + test "sends server events to the target client", %{socket: socket} do + push(socket, "connect", %{"session_token" => session_token(), "ref" => "1", "id" => "id1"}) + + assert_receive {:connect, from, %{}} + send(from, {:connect_reply, [1, 2, 3], %{ref: "1"}}) + + send(from, {:event, "ping", [1, 2, 3], %{ref: "1"}}) + assert_push "event:1", %{"root" => [["ping"], [1, 2, 3]]} + end + describe "binary payload" do test "initial data", %{socket: socket} do push(socket, "connect", %{"session_token" => session_token(), "ref" => "1", "id" => "id1"})