Skip to content

Commit 1b5cf64

Browse files
chasersfilipecabaco
authored andcommitted
feat: inspector now compatible with private channels and messages
1 parent 6c45a53 commit 1b5cf64

File tree

3 files changed

+43
-7
lines changed

3 files changed

+43
-7
lines changed

assets/js/app.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import { createClient } from "@supabase/supabase-js";
1111
let Hooks = {};
1212
Hooks.payload = {
1313
initRealtime(
14-
channelName,
14+
channel_name,
15+
private_channel,
1516
host,
1617
log_level,
1718
token,
@@ -33,6 +34,10 @@ Hooks.payload = {
3334
},
3435
};
3536

37+
const privateChannel = private_channel
38+
? private_channel.toLowerCase() === "true"
39+
: false;
40+
3641
this.realtimeSocket = createClient(host, token, opts);
3742

3843
if (bearer != "") {
@@ -42,8 +47,8 @@ Hooks.payload = {
4247
// Join the Channel 'any'
4348
// Channels can be named anything
4449
// All clients on the same Channel will get messages sent to that Channel
45-
this.channel = this.realtimeSocket.channel(channelName, {
46-
config: { broadcast: { self: true } },
50+
this.channel = this.realtimeSocket.channel(channel_name, {
51+
config: { broadcast: { self: true }, private: privateChannel },
4752
});
4853

4954
// Hack to confirm Postgres is subscribed
@@ -131,7 +136,7 @@ Hooks.payload = {
131136
localStorage.setItem("host", host);
132137
localStorage.setItem("token", token);
133138
localStorage.setItem("log_level", log_level);
134-
localStorage.setItem("channel", channelName);
139+
localStorage.setItem("channel", channel_name);
135140
localStorage.setItem("schema", schema);
136141
localStorage.setItem("table", table);
137142
localStorage.setItem("filter", filter);
@@ -187,12 +192,16 @@ Hooks.payload = {
187192
});
188193
},
189194

190-
sendRealtime(event, payload) {
195+
sendRealtime(event, payload, privateMessage) {
191196
// Send a `broadcast` message over the Channel
192197
// All connected clients will receive this message if they're subscribed
193198
// to `broadcast` events and matching on the `event` name or using `*` to match all event names
199+
200+
let priv = privateMessage ? privateMessage.toLowerCase() === "true" : false;
201+
194202
this.channel.send({
195203
type: "broadcast",
204+
private: priv,
196205
event: event,
197206
payload: payload,
198207
});
@@ -221,13 +230,15 @@ Hooks.payload = {
221230
bearer: localStorage.getItem("bearer"),
222231
enable_presence: localStorage.getItem("enable_presence"),
223232
enable_db_changes: localStorage.getItem("enable_db_changes"),
233+
private_channel: localStorage.getItem("private_channel"),
224234
};
225235

226236
this.pushEventTo("#conn_form", "local_storage", params);
227237

228238
this.handleEvent("connect", ({ connection }) =>
229239
this.initRealtime(
230240
connection.channel,
241+
connection.private_channel,
231242
connection.host,
232243
connection.log_level,
233244
connection.token,
@@ -241,7 +252,7 @@ Hooks.payload = {
241252
);
242253

243254
this.handleEvent("send_message", ({ message }) =>
244-
this.sendRealtime(message.event, message.payload)
255+
this.sendRealtime(message.event, message.payload, message.private_message)
245256
);
246257

247258
this.handleEvent("disconnect", ({}) => this.disconnectRealtime());

lib/realtime_web/live/inspector_live/conn_component.html.heex

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,19 @@
9292
<%= error_tag(f, :channel) %>
9393
<p class="text-gray-600 text-xs italic">The Channel to connect to</p>
9494
</div>
95+
<div class="mb-4">
96+
<%= label(f, :private_channel, class: "block text-gray-700 text-sm font-bold mb-2") %>
97+
<%= checkbox(f, :private_channel, class: "
98+
my-1
99+
block
100+
rounded-md
101+
border-gray-300
102+
shadow-sm
103+
focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50
104+
") %>
105+
<%= error_tag(f, :private_channel) %>
106+
<p class="text-gray-600 text-xs italic">Use RLS to enforce policies on this Channel</p>
107+
</div>
95108
<div class="mb-4">
96109
<%= label(f, :schema, class: "block text-gray-700 text-sm font-bold mb-2") %>
97110
<%= text_input(f, :schema, placeholder: "public", class: "

lib/realtime_web/live/inspector_live/index.html.heex

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,19 @@
102102
<%= error_tag(m, :payload) %>
103103
<p class="text-gray-600 text-xs italic">Message payload</p>
104104
</div>
105-
105+
<div class="mb-4">
106+
<%= label(m, :private_message, class: "block text-gray-700 text-sm font-bold mb-2") %>
107+
<%= checkbox(m, :private_message, class: "
108+
my-1
109+
block
110+
rounded-md
111+
border-gray-300
112+
shadow-sm
113+
focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50
114+
") %>
115+
<%= error_tag(m, :private_message) %>
116+
<p class="text-gray-600 text-xs italic">Private message</p>
117+
</div>
106118
<%= submit("Send",
107119
phx_disable_with: "Sending...",
108120
class:

0 commit comments

Comments
 (0)