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

Turbo Broadcasts #9

Open
wants to merge 2 commits into
base: turbo-frames-creates
Choose a base branch
from
Open

Turbo Broadcasts #9

wants to merge 2 commits into from

Conversation

piowit
Copy link
Collaborator

@piowit piowit commented Jul 18, 2024

Turbo Broadcasts

Links:
https://www.rubydoc.info/gems/turbo-rails/Turbo/Broadcastable
https://www.hotrails.dev/turbo-rails/turbo-streams

Task 1: Adding broadcasts to columns

1. Update app/views/boards/show.html.erb

Goal: Connect the user to a WebSocket channel to receive real-time updates.
Action: Add a turbo stream tag at the top of the file.

New line:
   <%= turbo_stream_from dom_id(@board) %>

Explanation:
This line of code sets up a Turbo Stream subscription to a WebSocket channel identified by the name created by dom_id(@board). Whenever something is broadcasted to this channel, the updates will be automatically reflected in the user's view.

2. Update app/models/board_column.rb

Goal: Enable the BoardColumn model to broadcast changes to the respective WebSocket channel.
Actions:

  • Include the ActionView::RecordIdentifier library to use the dom_id helper.
  • Add a broadcast callback to the model.
Updated file:
class BoardColumn < ApplicationRecord
include ActionView::RecordIdentifier

# ... leave old code

broadcasts_to ->(board_column) { "board_#{board_column.board_id}" },
          target: ->(board_column) { "columns_board_#{board_column.board.id}" },
          inserts_by: :append

Explanation:
The broadcasts_to method sets up the broadcasting for the BoardColumn model. It specifies that changes to BoardColumn records will be broadcasted to a channel named after the associated board_id ({ "board_#{board_column.board_id}" }). The target specifies where in the DOM the updates will be inserted (it will look for html id), and inserts_by: :append means new columns will be appended to the list.

Task 2: Triggering columns broadcasts on card changes

1. Update app/models/card.rb

Goal: Ensure that updates to cards trigger broadcasts for the associated columns, keeping the UI in sync.
Action: Add a callback to touch and update associated columns when cards are modified.

Updated file:
class Card < ApplicationRecord
   include ActionView::RecordIdentifier

   # ... leave old code

   after_commit :touch_affected_board_columns

   private

   def touch_affected_board_columns
      if previous_changes[:board_column_id].present?
         board.board_columns.find_by(id: previous_changes[:board_column_id]&.first)&.touch
         board.board_columns.find_by(id: previous_changes[:board_column_id]&.last)&.touch
      else
         board_column.touch
      end
   end
end

Explanation:
The after_commit callback is used to trigger the touch_affected_board_columns method after any changes to a Card record are committed to the database. This method checks if the board_column_id has changed, and if so, it touches the old and new columns to update their timestamps. This, in turn, triggers the broadcast, ensuring that any changes to cards are reflected in the columns in real-time.

@piowit piowit changed the title Add real-time updates for board views Turbo Broadcasts Jul 18, 2024
@maikhel maikhel force-pushed the turbo-frames-creates branch 2 times, most recently from e836765 to 0ab2840 Compare July 18, 2024 12:47
@piowit piowit force-pushed the turbo-broadcasts branch 3 times, most recently from 9aa3e8f to a0aacac Compare July 18, 2024 14:07
@maikhel maikhel force-pushed the turbo-frames-creates branch from 0ab2840 to c7f0ef6 Compare July 18, 2024 18:22
@maikhel maikhel force-pushed the turbo-broadcasts branch from a0aacac to 30702ed Compare July 18, 2024 18:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant