Skip to content

Commit e5c5b6f

Browse files
authored
Merge pull request rails#24991 from maclover7/actioncable-npm-docs
Add documentation about Action Cable npm package
2 parents 98131f7 + 1713075 commit e5c5b6f

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

actioncable/README.md

+68
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,74 @@ with all the popular application servers -- Unicorn, Puma and Passenger.
458458
Action Cable does not work with WEBrick, because WEBrick does not support the
459459
Rack socket hijacking API.
460460

461+
## Frontend assets
462+
463+
Action Cable's frontend assets are distributed through two channels: the
464+
official gem and npm package, both titled `actioncable`.
465+
466+
### Gem usage
467+
468+
Through the `actioncable` gem, Action Cable's frontend assets are
469+
available through the Rails Asset Pipeline. Create a `cable.js` or
470+
`cable.coffee` file (this is automatically done for you with Rails
471+
generators), and then simply require the assets:
472+
473+
In JavaScript...
474+
475+
```javascript
476+
//= require action_cable
477+
```
478+
479+
... and in CoffeeScript:
480+
481+
```coffeescript
482+
#= require action_cable
483+
```
484+
485+
### npm usage
486+
487+
In addition to being available through the `actioncable` gem, Action Cable's
488+
frontend JS assets are also bundled in an officially supported npm module,
489+
intended for usage in standalone frontend applications that communicate with a
490+
Rails application. A common use case for this could be if you have a decoupled
491+
frontend application written in React, Ember.js, etc. and want to add real-time
492+
WebSocket functionality.
493+
494+
### Installation
495+
496+
```
497+
npm install actioncable --save
498+
```
499+
500+
### Usage
501+
502+
The `ActionCable` constant is available as a `require`-able module, so
503+
you only have to require the package to gain access to the API that is
504+
provided.
505+
506+
In JavaScript...
507+
508+
```javascript
509+
ActionCable = require('actioncable')
510+
511+
var cable = ActionCable.createConsumer('wss://RAILS-API-PATH.com/cable')
512+
513+
cable.subscriptions.create('AppearanceChannel', {
514+
// normal channel code goes here...
515+
});
516+
```
517+
518+
and in CoffeeScript...
519+
520+
```coffeescript
521+
ActionCable = require('actioncable')
522+
523+
cable = ActionCable.createConsumer('wss://RAILS-API-PATH.com/cable')
524+
525+
cable.subscriptions.create 'AppearanceChannel',
526+
# normal channel code goes here...
527+
```
528+
461529
## License
462530

463531
Action Cable is released under the MIT license:

0 commit comments

Comments
 (0)