How to use ActionCable as API

前端 未结 2 898
囚心锁ツ
囚心锁ツ 2020-12-02 20:54

I built a very simple app using Rails 5 beta 1 and ActionCable to show when users come online and let them send messages to each other.

Now, I would basically like

2条回答
  •  臣服心动
    2020-12-02 21:24

    While mwalsher's solution was extremely helpful to me, I recently found a pull request on the Rails repository with an official solution to my question.

    https://github.com/rails/rails/pull/24991

    I assume, in the near future this will be added to the main documentation. Here is the link to the official actioncable npm package: https://www.npmjs.com/package/actioncable

    You can use it similar to mwalsher's solution with any JS app. Just install the npm package:

    npm install actioncable --save
    

    Here the JS example from the documentation:

    ActionCable = require('actioncable')
    
    var cable = ActionCable.createConsumer('wss://RAILS-API-PATH.com/cable')
    
    cable.subscriptions.create('AppearanceChannel', {
      // normal channel code goes here...
    });
    

    Edit: The pull request has been merged for a while, now and the description is part of the official Readme - just not yet in the Rails Guides.

提交回复
热议问题