Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: check for private key on public Channel and error #1125

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ This is the list of operational codes that can help you understand your deployme
| UnprocessableEntity | Received a HTTP request with a body that was not able to be processed by the endpoint |
| ErrorOnRpcCall | Error when calling another realtime node |
| ErrorExecutingTransaction | Error executing a database transaction in tenant database |
| PrivateKeyPublicChannelError | Incoming private message found on public Channel |
| UnknownError | An unknown error occurred |

## License
Expand Down
23 changes: 17 additions & 6 deletions assets/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import { createClient } from "@supabase/supabase-js";
let Hooks = {};
Hooks.payload = {
initRealtime(
channelName,
channel_name,
private_channel,
host,
log_level,
token,
Expand All @@ -33,6 +34,10 @@ Hooks.payload = {
},
};

const privateChannel = private_channel
? private_channel.toLowerCase() === "true"
: false;

this.realtimeSocket = createClient(host, token, opts);

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

// Hack to confirm Postgres is subscribed
Expand Down Expand Up @@ -131,7 +136,7 @@ Hooks.payload = {
localStorage.setItem("host", host);
localStorage.setItem("token", token);
localStorage.setItem("log_level", log_level);
localStorage.setItem("channel", channelName);
localStorage.setItem("channel", channel_name);
localStorage.setItem("schema", schema);
localStorage.setItem("table", table);
localStorage.setItem("filter", filter);
Expand Down Expand Up @@ -187,12 +192,16 @@ Hooks.payload = {
});
},

sendRealtime(event, payload) {
sendRealtime(event, payload, privateMessage) {
// Send a `broadcast` message over the Channel
// All connected clients will receive this message if they're subscribed
// to `broadcast` events and matching on the `event` name or using `*` to match all event names

let priv = privateMessage ? privateMessage.toLowerCase() === "true" : false;

this.channel.send({
type: "broadcast",
private: priv,
event: event,
payload: payload,
});
Expand Down Expand Up @@ -221,13 +230,15 @@ Hooks.payload = {
bearer: localStorage.getItem("bearer"),
enable_presence: localStorage.getItem("enable_presence"),
enable_db_changes: localStorage.getItem("enable_db_changes"),
private_channel: localStorage.getItem("private_channel"),
};

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

this.handleEvent("connect", ({ connection }) =>
this.initRealtime(
connection.channel,
connection.private_channel,
connection.host,
connection.log_level,
connection.token,
Expand All @@ -241,7 +252,7 @@ Hooks.payload = {
);

this.handleEvent("send_message", ({ message }) =>
this.sendRealtime(message.event, message.payload)
this.sendRealtime(message.event, message.payload, message.private_message)
);

this.handleEvent("disconnect", ({}) => this.disconnectRealtime());
Expand Down
Loading
Loading