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.
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
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.’});
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.