difficulty making client to server communication with action cable

32 views
Skip to first unread message

robert.p...@gmail.com

unread,
Apr 19, 2019, 7:38:19 AM4/19/19
to Ruby on Rails: Talk
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

Ariel Juodziukynas

unread,
Apr 19, 2019, 8:29:06 AM4/19/19
to rubyonra...@googlegroups.com
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.

--
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-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/72e7ca5b-11fa-4163-8626-a990f1b63bd8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Fatih Orhan

unread,
Apr 19, 2019, 10:52:18 PM4/19/19
to Ruby on Rails: Talk
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ı:

robert.p...@gmail.com

unread,
May 6, 2019, 1:23:36 PM5/6/19
to Ruby on Rails: Talk
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);
  }
});

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

Ariel Juodziukynas

unread,
May 6, 2019, 4:16:13 PM5/6/19
to rubyonra...@googlegroups.com
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.

--
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-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages