Ruby on Rails
Friday, April 19, 2019
When you send a message from client to server, the server doesn't automatically re-broadcast (echo) it. You need to handle message server-side, like this snipped from docs:
19 Nisan 2019 Cuma 14:38:19 UTC+3 tarihinde robert.p...@gmail.com yazdı:
-- # app/channels/chat_channel.rb
class
ChatChannel < ApplicationCable::Channel
def
subscribed
stream_from
"chat_#{params[:room]}"
end
def
receive(data)
ActionCable.server.broadcast(
"chat_#{params[:room]}"
, data)
end
end
Check out the guides if you haven't: https://guides.rubyonrails.org/action_cable_overview.html
19 Nisan 2019 Cuma 14:38:19 UTC+3 tarihinde robert.p...@gmail.com yazdı:
I am trying to use action cable.I got the server to send to the client. But I also want the client to send to the server.I tried this in my coffeescript fileApp.hasdfg = App.cable.subscriptions.create "HasdfgChannel", connected: -> # Called when the subscription is ready for use on the server disconnected: -> # Called when the subscription has been terminated by the server received: (data) -> # Called when there's incoming data on the websocket for this channel alert(data); #App.hasdfg.send # sent_by: 'Paul' # body: 'This is a cool chat app.' #App.hasdfg.send # "yyy" App.hasdfg.send 'This is a cool chat app.'but that last line isn't workingthat last line doesn't seem to cause an alert, so it doesn't seem to trigger the received methodI know the alert line works though because when sending a message from server to client, I trigger it. (I have a line in my controller broadcasting a message to the channel and that triggers the alert fine).It's just that the last line in the coffeescript, which is meant to send from client to server, is not triggering that received method, / is not causing an alert.Thanks
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/90f768af-019e-4c13-9b35-d554114c9a44%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment