how to send message to all client except sender in rails/actioncable?

前端 未结 2 2160
面向向阳花
面向向阳花 2021-02-07 14:20

in socket.io, you can send message to all client except sender like:

socket.broadcast.emit(\'user connected\');

but in rails/actioncable, how t

2条回答
  •  無奈伤痛
    2021-02-07 15:06

    Using jobs, you can make this with the logic in a partial.
    In your model after create a record, call the job perform passing the self record. See bellow how pass message to recieved data(at channel) using broadcast.

       ...  
    
       def perform(message)
         ActionCable.server.broadcast "board:#{params[:board]}",
         message: render_message(message)
       end
    
       private
    
       def 
         ApplicationController.renderer.render(
           partial: 'messages/message.html.erb',
           locals: { message: message }
         )
       end  
    

    Create a partial in views/messages/_message.html.erb and in your code make

     <%= if message.sender_id == current_user.id %>
       The code to report that forwarded message.
     <%= else %>
       The code to send the message to all except sender.
     <% end %>
    

提交回复
热议问题