Skip to content

Commit

Permalink
Replace ListChannel and WorkspaceChannel with signed streams
Browse files Browse the repository at this point in the history
  • Loading branch information
palkan committed Apr 2, 2024
1 parent 7b2111f commit e81aa59
Show file tree
Hide file tree
Showing 14 changed files with 20 additions and 127 deletions.
2 changes: 2 additions & 0 deletions .dockerdev/compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ x-backend: &backend
ANYCABLE_RPC_HOST: 0.0.0.0:50051
ANYCABLE_BROADCAST_ADAPTER: http
ANYCABLE_HTTP_BROADCAST_URL: http://ws:8090/_broadcast
ANYCABLE_STREAMS_SECRET: secr3t
CHROME_URL: ${CHROME_URL:-http://chrome:3333}
EDITOR: vi
LOG: ${LOG:-0}
Expand Down Expand Up @@ -101,6 +102,7 @@ services:
environment: &ws_environment
ANYCABLE_HOST: "0.0.0.0"
ANYCABLE_PORT: 8080
ANYCABLE_STREAMS_SECRET: secr3t
ANYCABLE_REDIS_URL: redis://redis:6379/0
ANYCABLE_RPC_HOST: anycable:50051
ANYCABLE_BROADCAST_ADAPTER: http
Expand Down
16 changes: 0 additions & 16 deletions app/channels/list_channel.rb

This file was deleted.

13 changes: 0 additions & 13 deletions app/channels/workspace_channel.rb

This file was deleted.

6 changes: 3 additions & 3 deletions app/controllers/items_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ def set_item

def broadcast_new_item
return if item.errors.any?
ListChannel.broadcast_to list, {type: "created", html: render_to_string(partial: "items/item", layout: false, locals: {item:}), id: item.id}
ActionCable.server.broadcast list, {type: "created", html: render_to_string(partial: "items/item", layout: false, locals: {item:}), id: item.id}
end

def broadcast_changes
return if item.errors.any?
if item.destroyed?
ListChannel.broadcast_to list, {type: "deleted", id: item.id}
ActionCable.server.broadcast list, {type: "deleted", id: item.id}
else
ListChannel.broadcast_to list, {type: "updated", id: item.id, desc: item.desc, completed: item.completed}
ActionCable.server.broadcast list, {type: "updated", id: item.id, desc: item.desc, completed: item.completed}
end
end
end
4 changes: 2 additions & 2 deletions app/controllers/lists_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ def set_list

def broadcast_new_list
return if list.errors.any?
WorkspaceChannel.broadcast_to workspace, {type: "newList", html: render_to_string(partial: "lists/list", layout: false, locals: {list:}), id: list.id}
ActionCable.server.broadcast workspace, {type: "newList", html: render_to_string(partial: "lists/list", layout: false, locals: {list:}), id: list.id}
end

def broadcast_changes
return if list.errors.any?
if list.destroyed?
WorkspaceChannel.broadcast_to workspace, {type: "deletedList", id: list.id}
ActionCable.server.broadcast workspace, {type: "deletedList", id: list.id}
end
end
end
2 changes: 1 addition & 1 deletion app/views/lists/_list.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div class="any-list--panel" id="<%= dom_id(list) %>" data-controller="list" data-list-id="<%= list.id %>" data-list-workspace="<%= list.workspace.to_param %>">
<div class="any-list--panel" id="<%= dom_id(list) %>" data-controller="list" data-list-stream="<%= signed_stream_name(list) %>">
<div id="<%= dom_id(list, :header) %>">
<%= form_for [list.workspace, list], method: :delete, html: {data: {confirm: "Are you sure you want to delete #{list.name} list?"}, class: "sticky top-0 bg-white pt-6 flex justify-between"} do |f|%>
<h3 class="truncate"><%= list.name %></h3>
Expand Down
2 changes: 1 addition & 1 deletion app/views/workspaces/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="px-6" data-controller="workspaces--remember" data-workspaces--remember-url="<%= workspace_path(workspace) %>" data-workspaces--remember-name="<%= workspace.name %>">
<div class="grid grid-cols-1 lg:grid-cols-chat gap-4" data-controller="workspace" data-workspace-id="<%= workspace.to_param %>">
<div class="grid grid-cols-1 lg:grid-cols-chat gap-4" data-controller="workspace" data-workspace-stream="<%= signed_stream_name(workspace) %>">
<%= render "chats/chat", workspace: workspace %>
<div class="grid grid-cols-1 lg:grid-cols-3 gap-4" id="<%= dom_id(workspace, :lists) %>" data-target="workspace.lists">
<div class="any-banner mb-4" role="alert" data-controller="banner" data-banner-id="todo">
Expand Down
2 changes: 2 additions & 0 deletions config/anycable.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ default: &default
redis_channel: "__anycable__"
# WebSocket server URL for clients
websocket_url: "ws://localhost:8080/cable"
# Secret used to generate signed streams
streams_secret: "secr3t"

development:
<<: *default
Expand Down
5 changes: 2 additions & 3 deletions frontend/controllers/list_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ export default class extends Controller {
connect() {
if (isTurboPreview()) return;

const id = this.data.get("id");
const workspace = this.data.get("workspace");
const stream = this.data.get("stream");

const cable = createCable();

this.channel = cable.subscribeTo("ListChannel", { id, workspace });
this.channel = cable.streamFromSigned(stream);
this.channel.on("message", (data) => this.handleUpdate(data));
}

Expand Down
4 changes: 2 additions & 2 deletions frontend/controllers/workspace_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ export default class extends Controller {
connect() {
if (isTurboPreview()) return;

const id = this.data.get("id");
const stream = this.data.get("stream");

const cable = createCable();

this.channel = cable.subscribeTo("WorkspaceChannel", { id });
this.channel = cable.streamFromSigned(stream);
this.channel.on("message", (data) => this.handleUpdate(data));
}

Expand Down
47 changes: 0 additions & 47 deletions spec/channels/list_channel_spec.rb

This file was deleted.

34 changes: 0 additions & 34 deletions spec/channels/workspace_channel_spec.rb

This file was deleted.

6 changes: 3 additions & 3 deletions spec/controllers/items_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
end

it "broadcasts a created message" do
expect { subject }.to have_broadcasted_to(ListChannel.broadcasting_for(list))
expect { subject }.to have_broadcasted_to(AnyCable::Rails.stream_name_from(list))
.with(a_hash_including(type: "created"))
end
end
Expand All @@ -36,7 +36,7 @@
end

it "broadcasts a deleted message" do
expect { subject }.to have_broadcasted_to(ListChannel.broadcasting_for(list))
expect { subject }.to have_broadcasted_to(AnyCable::Rails.stream_name_from(list))
.with(type: "deleted", id: item.id)
end
end
Expand All @@ -49,7 +49,7 @@
subject { patch :update, params: {workspace_id: workspace.to_param, list_id: list.id, id: item.id, item: form_params} }

it "broadcasts an updated message" do
expect { subject }.to have_broadcasted_to(ListChannel.broadcasting_for(list))
expect { subject }.to have_broadcasted_to(AnyCable::Rails.stream_name_from(list))
.with(type: "updated", id: item.id, completed: true, desc: item.desc)
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/controllers/lists_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
end

it "broadcasts a newList message" do
expect { subject }.to have_broadcasted_to(WorkspaceChannel.broadcasting_for(workspace))
expect { subject }.to have_broadcasted_to(AnyCable::Rails.stream_name_from(workspace))
.with(a_hash_including(type: "newList"))
end
end
Expand All @@ -35,7 +35,7 @@
end

it "broadcasts a deletedList message" do
expect { subject }.to have_broadcasted_to(WorkspaceChannel.broadcasting_for(workspace))
expect { subject }.to have_broadcasted_to(AnyCable::Rails.stream_name_from(workspace))
.with(type: "deletedList", id: list.id)
end
end
Expand Down

0 comments on commit e81aa59

Please sign in to comment.