Consuming Rails 5 ActionCable

前端 未结 1 1144
名媛妹妹
名媛妹妹 2021-01-16 23:38

I\'m playing around with the new ActionCable feature. Is there a way of communicating with the ActionCable server using say socket.io or from an application using React or R

相关标签:
1条回答
  • 2021-01-17 00:26

    You can interact with ActionCable as you would normally with any WebSocket libraries.

    To do so, you would stream from a Channel in Rails:

    class ExampleChannel < ApplicationCable::Channel
      def subscribe
        stream_from 'example'
      end
    end
    

    Then, you may connect to the Rails WebSocket through your stand-alone client and subscribe to the message using the ActionCable protocol:

    function Socket(url) {
      const ws = new WebSocket(url);
    
      ws.onopen(() => {
        ws.send('{"command":"subscribe","identifier":"{\"channel\":\"ExampleChannel\"}"');
      });
    }
    

    Reference: http://edgeguides.rubyonrails.org/action_cable_overview.html#channels

    https://github.com/NullVoxPopuli/action_cable_client/blob/master/README.md

    0 讨论(0)
提交回复
热议问题