difficulty making client to server communication with action cable

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 file

  App.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 working

that last line doesn’t seem to cause an alert, so it doesn’t seem to trigger the received method

I 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

The received method is called when the client receives data from the server, if you want to know if the client is sending the data properly to the server check the server log. The received method is not supposed to be called when the client sends a message to the server.

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: # 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ı:

Thanks for the tips

I got it working making sure that at the client I put the send line in the connect function so it didn’t try to send before connecting, and I passed in a dictionary object.

app/assets/javascripts/theawpchan.js

App.awpchan.send({message: ‘This is a cool chat app.’});

App.awpchan = App.cable.subscriptions.create(“AwpchanChannel”, {

connected: function() {

App.awpchan.send({message: ‘This is a cool chat app.’});

},

disconnected: function() {},

received: function(data) {

return alert(data);

}

});

// http://js2.coffee/

And I added the receive method at the server side…

app/channels/awpchan_channel.rb

class AwpchanChannel < ApplicationCable::Channel

def subscribed

stream_from “abcdstrm”

end

def unsubscribed

Any cleanup needed when channel is unsubscribed

end

def receive(data)

puts data[“message”]

end

end

That first “App.awpchan.send({message: ‘This is a cool chat app.’});” does not make sense. Did you edit my answer on stackoverflow? It looks messed up, why did you change it? I had both issues split on two parts on my answer on purpose to be explicit on which fixes which issue. I don’t think the actual edit is an improvement on my original answer.