xlaok
xlaok

Reputation: 597

how to reply message to the sender client with actioncable?

in socket.io, you can reply message to sender like:

 socket.on('records', function(sheet_id){
    records = Record.all
    //send message to sender
    socket.emit('records', records);
  });

but in rails:

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

   def speak
     # client will call @perform('speak')
     result = do_something()
     # how to send 'result' to sender?
   end
 end

Upvotes: 2

Views: 1162

Answers (1)

gamendola
gamendola

Reputation: 144

Use ActionCable::Connection::Base#transmit method

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

  def speak
    result = do_something()
    transmit(result) # send message to current connection (sender)
  end
end

Upvotes: 6

Related Questions