Skip to content

Commit

Permalink
Fixes #20 the broadcasts will not go to INSERT, UPDATE, DELETE too.
Browse files Browse the repository at this point in the history
This also cleans up the code a bit - the channel will not accept inbound messages. I can't imagine that it will ever need it. It may in fact be a security flaw to allow it
  • Loading branch information
kiwicopple committed Feb 25, 2020
1 parent 22d941e commit dd7d6e5
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 16 deletions.
2 changes: 1 addition & 1 deletion NOTICE
Original file line number Diff line number Diff line change
@@ -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).
2 changes: 1 addition & 1 deletion server/lib/realtime/replication.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 9 additions & 8 deletions server/lib/realtime_web/channels/realtime_channel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
37 changes: 31 additions & 6 deletions server/test/realtime_web/channels/realtime_channel_test.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
defmodule RealtimeWeb.RealtimeChannelTest do
use RealtimeWeb.ChannelCase
require Logger

setup do
{:ok, _, socket} =
Expand All @@ -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

0 comments on commit dd7d6e5

Please sign in to comment.