diff --git a/NOTICE b/NOTICE index cc6d723bc..9e6901cd5 100644 --- a/NOTICE +++ b/NOTICE @@ -1,4 +1,4 @@ Supabase Realtime -Copyright 2019 Supabasae. +Copyright 2019 Supabase. This software contains code derived from the Cainophile (https://github.com/cainophile/cainophile). diff --git a/server/lib/realtime/replication.ex b/server/lib/realtime/replication.ex index 5a91b6234..7c6d48e5d 100644 --- a/server/lib/realtime/replication.ex +++ b/server/lib/realtime/replication.ex @@ -236,7 +236,7 @@ defmodule Realtime.Replication do # Shout to specific columns - e.g. "realtime:public:users.id=eq.2" Enum.each change.record, fn {k, v} -> eq = table_topic <> ":" <> k <> "=eq." <> v - Logger.info inspect(eq) + Logger.info inspect(eq) RealtimeWeb.RealtimeChannel.handle_realtime_transaction(eq, change) end end diff --git a/server/lib/realtime_web/channels/realtime_channel.ex b/server/lib/realtime_web/channels/realtime_channel.ex index 2b1308003..fe370bfa3 100644 --- a/server/lib/realtime_web/channels/realtime_channel.ex +++ b/server/lib/realtime_web/channels/realtime_channel.ex @@ -15,20 +15,21 @@ defmodule RealtimeWeb.RealtimeChannel do end end + @doc """ + Disabling inward messages from the websocket. + """ + # def handle_in(event_type, payload, socket) do + # Logger.info event_type + # broadcast!(socket, event_type, payload) + # {:noreply, socket} + # end - # It is also common to receive messages from the client and - # broadcast to everyone in the current topic (realtime:lobby). - def handle_in("*", payload, socket) do - broadcast!(socket, "*", payload) - {:noreply, socket} - end @doc """ Handles a full, decoded transation. """ def handle_realtime_transaction(topic, txn) do - # Logger.info 'REALTIME!' - # Logger.info inspect(txn, pretty: true) RealtimeWeb.Endpoint.broadcast_from!(self(), topic, "*", txn) + RealtimeWeb.Endpoint.broadcast_from!(self(), topic, txn.type, txn) end end diff --git a/server/test/realtime_web/channels/realtime_channel_test.exs b/server/test/realtime_web/channels/realtime_channel_test.exs index 10f1249db..f449e2f0f 100644 --- a/server/test/realtime_web/channels/realtime_channel_test.exs +++ b/server/test/realtime_web/channels/realtime_channel_test.exs @@ -1,5 +1,6 @@ defmodule RealtimeWeb.RealtimeChannelTest do use RealtimeWeb.ChannelCase + require Logger setup do {:ok, _, socket} = @@ -9,13 +10,37 @@ defmodule RealtimeWeb.RealtimeChannelTest do {:ok, socket: socket} end - test "shout broadcasts to realtime", %{socket: socket} do - push socket, "*", %{"hello" => "all"} - assert_broadcast "*", %{"hello" => "all"} + test "INSERTS are broadcasts to the client", %{socket: socket} do + change = %{ + schema: "public", + table: "users", + type: "INSERT" + } + RealtimeWeb.RealtimeChannel.handle_realtime_transaction("realtime:*", change) + assert_push("*", change) + assert_push("INSERT", change) + end + + test "UPDATES are broadcasts to the client", %{socket: socket} do + change = %{ + schema: "public", + table: "users", + type: "UPDATES" + } + RealtimeWeb.RealtimeChannel.handle_realtime_transaction("realtime:*", change) + assert_push("*", change) + assert_push("UPDATES", change) end - test "broadcasts are pushed to the client", %{socket: socket} do - broadcast_from! socket, "broadcast", %{"some" => "data"} - assert_push "broadcast", %{"some" => "data"} + test "DELETES are broadcasts to the client", %{socket: socket} do + change = %{ + schema: "public", + table: "users", + type: "DELETE" + } + RealtimeWeb.RealtimeChannel.handle_realtime_transaction("realtime:*", change) + assert_push("*", change) + assert_push("DELETE", change) end + end