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

前端 未结 2 2117
面向向阳花
面向向阳花 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:11

    I have been worrying about this problem all afternoon. All ready to give up, just lying in bed, my mind flashed a solution it works! ! ! Sign in to share

     class BoardChannel < ApplicationCable::Channel
       def subscribed
         stream_from "board:#{params[:board]}"
         stream_from "global_stream"
       end
     end
    

    Then, when you want to broadcast all users, you can:

    ActionCable.server.broadcast "global_stream", "some messages"
    

    Of course, you can also broadcast to special users. you can:

    ActionCable.server.broadcast "board:#{params[:board]}", "some messages"
    
    

    Perfect!!!

提交回复
热议问题