Skip to content

Commit

Permalink
Migrate to broadcasting to others
Browse files Browse the repository at this point in the history
  • Loading branch information
palkan committed Oct 16, 2023
1 parent dc21f05 commit 63c60a5
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 5 deletions.
4 changes: 1 addition & 3 deletions app/controllers/messages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ def create
# Set current region to broadcast to others
Current.fly_region = Rails.application.config.x.fly_region if Rails.application.config.x.fly_debug

current_channel.messages.create(message_params.merge(user: current_user))

render partial: "messages/form", locals: {channel: current_channel}
@message = current_channel.messages.create(message_params.merge(user: current_user))
end

private
Expand Down
2 changes: 1 addition & 1 deletion app/javascript/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import "@hotwired/turbo"

import { start } from "@anycable/turbo-stream"
import cable from "cable"
start(cable)
start(cable, { requestSocketIDHeader: true })

import "controllers"
4 changes: 3 additions & 1 deletion app/models/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ class Message < ApplicationRecord
belongs_to :user

after_commit on: :create do
broadcast_append_to channel, partial: "messages/message", locals: {message: self}, target: "messages"
AnyCable::Rails.broadcasting_to_others do
broadcast_append_to channel, partial: "messages/message", locals: {message: self}, target: "messages"
end
end
end
7 changes: 7 additions & 0 deletions app/views/messages/create.turbo_stream.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<%= turbo_stream.replace dom_id(current_channel, :form) do %>
<%= render partial: "messages/form", locals: { channel: current_channel } %>
<% end %>
<%= turbo_stream.append :messages do %>
<%= render partial: "messages/message", locals: {message: @message} %>
<% end %>

0 comments on commit 63c60a5

Please sign in to comment.