Skip to content

[demo-4] WebSocketを使ったブロードキャスト方式に変更#11

Open
yakitorii wants to merge 3 commits into3-use-streamfrom
4-use-websocket
Open

[demo-4] WebSocketを使ったブロードキャスト方式に変更#11
yakitorii wants to merge 3 commits into3-use-streamfrom
4-use-websocket

Conversation

@yakitorii
Copy link
Copy Markdown
Member

@yakitorii yakitorii commented Dec 12, 2022

やったこと

  • Tweetの追加をブロードキャスト方式に
    • docker-compose にredisを追加
    • Emotionモデルにafter_create_commit -> { broadcast_prepend_to("emotions") }によるブロードキャストを追加
    • index.html.erb にemotionsの変更をサブスクライブする<%= turbo_stream_from "emotions" %>を追加
    • create.turbo_stream.erbでの、emotion追加時のprependを削除
  • 削除もそちらに対応
    • Emotionモデルにafter_destroy_commit -> { broadcast_remove_to("emotions") }によるブロードキャストを追加
    • _emotion.html.erbで、内容を<%= turbo_frame_tag emotion do %> で囲むことで各emotionに個別のidをもつturbo-frameタグを追加

確認方法

二つのブラウザをひらいて追加、削除をしてみて、別のブラウザで即時反映される

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.

2 participants