I have a react frontend(not using the react-rails gem) and i want to use Rails api for realtime interaction. I have this post.coffee
code in my rails already working for ActionCable:
App.post = App.cable.subscriptions.create "PostChannel",
connected: ->
disconnected: ->
received: (data) ->
console.log(data)
document.getElementById('test').innerHTML = data['message']
notify: (message) ->
@perform 'notify', message: message
and this post_channel.rb
class PostChannel < ApplicationCable::Channel
def subscribed
stream_from "post_channel"
end
def unsubscribed
# Any cleanup needed when channel is unsubscribed
end
def notify(data)
ActionCable.server.broadcast 'post_channel', message: data['message']
end
end
working example:
now in my react app, lets say on page load [ componentWillMount() ], I want to 'call' the notify method from the post_channel.rb. How will I do it? I have searched some method and saw this post's accepted answer: How to use ActionCable as API
So in my react app's componentWillMount() lifecycle, I have this code:
componentWillMount() {
var cable = ActionCable.createConsumer('ws://localhost:8000/cable');
cable.server.broadcast 'post_channel', { message: 'ji'}
}
I got the ws url
from my development.rb:
The code in componentWillMount does not even call the websocket in my rails app. How to do this? I dont see much resource. or could you recommend any websocket library best for react-rails stack?