質問編集履歴
1
画像をテキストに変えました
    
        title	
    CHANGED
    
    | 
            File without changes
         | 
    
        body	
    CHANGED
    
    | @@ -2,16 +2,157 @@ | |
| 2 2 | 
             
            リアルタイムチャットでメッセージを受信したユーザに通知を送る機能を実装したいと考えているのですが、方法が思いつきません。
         | 
| 3 3 |  | 
| 4 4 | 
             
            もし分かる方がいれば、お力をお借りしたいです。
         | 
| 5 | 
            -
            以下のようなコードとなっています。
         | 
| 6 5 |  | 
| 7 | 
            -
             | 
| 6 | 
            +
            ずっと困っているので、助けていただければ本当に助かります!
         | 
| 8 | 
            -
             | 
| 7 | 
            +
            どうか宜しくお願いいたします。
         | 
| 9 8 |  | 
| 10 | 
            -
            
         | 
| 11 | 
            -
            
         | 
| 12 | 
            -
             | 
| 9 | 
            +
            以下のようなコードとなっております。
         | 
| 13 | 
            -
            
         | 
| 14 | 
            -
            
         | 
| 15 10 |  | 
| 11 | 
            +
            ```ruby
         | 
| 12 | 
            +
            class RoomChannel < ApplicationCable::Channel
         | 
| 13 | 
            +
              def subscribed
         | 
| 14 | 
            +
                stream_from "room_channel_#{params['room']}"
         | 
| 15 | 
            +
              end
         | 
| 16 | 
            +
             | 
| 17 | 
            +
              def unsubscribed
         | 
| 18 | 
            +
                # Any cleanup needed when channel is unsubscribed
         | 
| 19 | 
            +
              end
         | 
| 20 | 
            +
             | 
| 21 | 
            +
              def speak(data)
         | 
| 22 | 
            +
                # jsで実行されたspeakのmessageを受け取り、room_channelのreceivedにブロードキャストする
         | 
| 23 | 
            +
                Message.create! content: data['message'], user_id: current_user.id, room_id: params['room']
         | 
| 24 | 
            +
              end
         | 
| 25 | 
            +
            end
         | 
| 26 | 
            +
             | 
| 27 | 
            +
            ```
         | 
| 28 | 
            +
             | 
| 29 | 
            +
             | 
| 30 | 
            +
             | 
| 31 | 
            +
            ```ruby
         | 
| 32 | 
            +
            class MessageBroadcastJob < ApplicationJob
         | 
| 33 | 
            +
              queue_as :default
         | 
| 34 | 
            +
             | 
| 35 | 
            +
              def perform(message)
         | 
| 36 | 
            +
                ActionCable.server.broadcast "room_channel_#{message.room_id}", message: render_message(message)
         | 
| 37 | 
            +
              end
         | 
| 38 | 
            +
             | 
| 39 | 
            +
              private
         | 
| 40 | 
            +
             | 
| 41 | 
            +
              def render_message(message)
         | 
| 42 | 
            +
                 ApplicationController.render_with_signed_in_user(message.user, partial: 'messages/message', locals: { message: message })
         | 
| 43 | 
            +
              end
         | 
| 44 | 
            +
            end
         | 
| 45 | 
            +
            ```
         | 
| 46 | 
            +
            ```Ruby
         | 
| 47 | 
            +
            module ApplicationCable
         | 
| 48 | 
            +
              class Connection < ActionCable::Connection::Base
         | 
| 49 | 
            +
                identified_by :current_user
         | 
| 50 | 
            +
             | 
| 51 | 
            +
                def connect
         | 
| 52 | 
            +
                  reject_unauthorized_connection unless find_verified_user
         | 
| 53 | 
            +
                end
         | 
| 54 | 
            +
             | 
| 55 | 
            +
                private
         | 
| 56 | 
            +
             | 
| 57 | 
            +
                def find_verified_user
         | 
| 58 | 
            +
                  self.current_user = env['warden'].user
         | 
| 59 | 
            +
                end
         | 
| 60 | 
            +
              end
         | 
| 61 | 
            +
            end
         | 
| 62 | 
            +
            ```
         | 
| 63 | 
            +
             | 
| 64 | 
            +
            ```Ruby
         | 
| 65 | 
            +
            class Message < ApplicationRecord
         | 
| 66 | 
            +
              validates :content, presence: true
         | 
| 67 | 
            +
              after_create_commit { MessageBroadcastJob.perform_later self }
         | 
| 68 | 
            +
              belongs_to :user
         | 
| 69 | 
            +
              belongs_to :room
         | 
| 70 | 
            +
              has_many :active_notifications, class_name: 'Notification', foreign_key: 'visitor_id', dependent: :destroy
         | 
| 71 | 
            +
              has_many :passive_notifications, class_name: 'Notification', foreign_key: 'visited_id', dependent: :destroy
         | 
| 72 | 
            +
            end
         | 
| 73 | 
            +
             | 
| 74 | 
            +
            ```
         | 
| 75 | 
            +
             | 
| 76 | 
            +
            ```Ruby
         | 
| 77 | 
            +
            class MessagesController < ApplicationController
         | 
| 78 | 
            +
            end
         | 
| 79 | 
            +
             | 
| 80 | 
            +
            ```
         | 
| 81 | 
            +
             | 
| 82 | 
            +
            ```JavaScript
         | 
| 83 | 
            +
            import consumer from './consumer'
         | 
| 84 | 
            +
             | 
| 85 | 
            +
            // $(function() { ... }); で囲むことでレンダリング後に実行される
         | 
| 86 | 
            +
            // レンダリング前に実行されると $('#messages').data('room_id') が取得できない
         | 
| 87 | 
            +
            // turbolinks を使っている場合は $(document).on('turbolinks:load', function() { ... }); で囲う
         | 
| 88 | 
            +
            $(function() {
         | 
| 89 | 
            +
              const chatChannel = consumer.subscriptions.create({ channel: 'RoomChannel', room: $('#messages').data('room_id') }, {
         | 
| 90 | 
            +
                connected() {
         | 
| 91 | 
            +
                  // Called when the subscription is ready for use on the server
         | 
| 92 | 
            +
                },
         | 
| 93 | 
            +
             | 
| 94 | 
            +
                disconnected() {
         | 
| 95 | 
            +
                  // Called when the subscription has been terminated by the server
         | 
| 96 | 
            +
                },
         | 
| 97 | 
            +
             | 
| 98 | 
            +
                received: function(data) {
         | 
| 99 | 
            +
                  return $('#messages').append(data['message']);
         | 
| 100 | 
            +
                },
         | 
| 101 | 
            +
             | 
| 102 | 
            +
                speak: function(message) {
         | 
| 103 | 
            +
                  return this.perform('speak', {
         | 
| 104 | 
            +
                    message: message
         | 
| 105 | 
            +
                  });
         | 
| 106 | 
            +
                }
         | 
| 107 | 
            +
              });
         | 
| 108 | 
            +
             | 
| 109 | 
            +
              $(document).on('keypress', '[data-behavior~=room_speaker]', function(event) {
         | 
| 110 | 
            +
                if (event.keyCode === 13) {
         | 
| 111 | 
            +
                  chatChannel.speak(event.target.value);
         | 
| 112 | 
            +
                  event.target.value = '';
         | 
| 113 | 
            +
                  return event.preventDefault();
         | 
| 114 | 
            +
                }
         | 
| 115 | 
            +
              });
         | 
| 116 | 
            +
             | 
| 117 | 
            +
                $(".button").on("click",function(){
         | 
| 118 | 
            +
                  var message = $(".speaker_txt").val();
         | 
| 119 | 
            +
                  chatChannel.speak(message);
         | 
| 120 | 
            +
                  $(".speaker_txt").val("")
         | 
| 121 | 
            +
              });
         | 
| 122 | 
            +
            });
         | 
| 123 | 
            +
             | 
| 124 | 
            +
            ```
         | 
| 125 | 
            +
             | 
| 126 | 
            +
             | 
| 127 | 
            +
            ```HTML
         | 
| 128 | 
            +
            <div class="chat-room-container">
         | 
| 129 | 
            +
            <%= link_to rooms_path do%>
         | 
| 130 | 
            +
              <i class="fas fa-arrow-left fa-2x" aria-hidden="true"></i>
         | 
| 131 | 
            +
            <% end %>
         | 
| 132 | 
            +
            <% if @user.image? %>
         | 
| 133 | 
            +
              <%= link_to user_path(@user) do %>
         | 
| 134 | 
            +
                  <%= image_tag(@user.image.to_s, :class => "top-image")%>
         | 
| 135 | 
            +
              <% end %>
         | 
| 136 | 
            +
              <span class="name"><%= @user.name %></span>
         | 
| 137 | 
            +
            <% else %>
         | 
| 138 | 
            +
              <%= link_to user_path(@user) do %>
         | 
| 139 | 
            +
                <%= image_tag("default.png", :class => "top-image")%>
         | 
| 140 | 
            +
              <% end %>
         | 
| 141 | 
            +
              <span class="name"><%= @user.name %></span>
         | 
| 142 | 
            +
            <% end %>
         | 
| 143 | 
            +
            <hr>
         | 
| 144 | 
            +
            <div class= "messages" id ='messages' data-room_id="<%= @room.id %>">
         | 
| 16 | 
            -
             | 
| 145 | 
            +
              <%= render @messages %>
         | 
| 17 | 
            -
             | 
| 146 | 
            +
            </div>
         | 
| 147 | 
            +
            </div>
         | 
| 148 | 
            +
            <div class= "message-form row">
         | 
| 149 | 
            +
            <div class = "send-form offset-1 col-9">
         | 
| 150 | 
            +
            <form>
         | 
| 151 | 
            +
              <input class="speaker_txt col-12" name="content" type="text" data-behavior="room_speaker", autofocus>
         | 
| 152 | 
            +
            </form>
         | 
| 153 | 
            +
            </div>
         | 
| 154 | 
            +
            <div class= "send-button col-1">
         | 
| 155 | 
            +
            <button class="button btn btn-primary"><i class="fas fa-paper-plane"></i></button>
         | 
| 156 | 
            +
            </div>
         | 
| 157 | 
            +
            </div>
         | 
| 158 | 
            +
            ```
         | 
