difficulty making client to server communication with action cable

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

difficulty making client to server communication with action cable

robert.phillips37
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

--
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 [hidden email].
To post to this group, send email to [hidden email].
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.
Reply | Threaded
Open this post in threaded view
|

Re: difficulty making client to server communication with action cable

Ariel Juodziukynas
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.

El vie., 19 abr. 2019 08:38, <[hidden email]> escribió:
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

--
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 [hidden email].
To post to this group, send email to [hidden email].
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.

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAPS3bcAkdo6xw_dh%2Bi691eEa2921rzWenmJ80EL6A0xaddCoLA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: difficulty making client to server communication with action cable

Fatih Orhan
In reply to this post by robert.phillips37
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 [hidden email] 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 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

--
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 [hidden email].
To post to this group, send email to [hidden email].
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.
Reply | Threaded
Open this post in threaded view
|

Re: difficulty making client to server communication with action cable

robert.phillips37
In reply to this post by robert.phillips37
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

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/3cc4c9bf-c54b-4ca5-bb64-3f36da22837a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: difficulty making client to server communication with action cable

Ariel Juodziukynas
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.

El lun., 6 may. 2019 a las 14:24, <[hidden email]> escribió:
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

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/3cc4c9bf-c54b-4ca5-bb64-3f36da22837a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAPS3bcAFZHabL3wLy6J0%3DpwRb81NHhKs%2Bqwi%3DMVQfAaHSFrZyA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.