actioncable 0.0.0 → 5.0.0.beta1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +8 -0
- data/MIT-LICENSE +20 -0
- data/README.md +439 -21
- data/lib/action_cable.rb +47 -2
- data/lib/action_cable/channel.rb +14 -0
- data/lib/action_cable/channel/base.rb +277 -0
- data/lib/action_cable/channel/broadcasting.rb +29 -0
- data/lib/action_cable/channel/callbacks.rb +35 -0
- data/lib/action_cable/channel/naming.rb +22 -0
- data/lib/action_cable/channel/periodic_timers.rb +41 -0
- data/lib/action_cable/channel/streams.rb +114 -0
- data/lib/action_cable/connection.rb +16 -0
- data/lib/action_cable/connection/authorization.rb +13 -0
- data/lib/action_cable/connection/base.rb +221 -0
- data/lib/action_cable/connection/identification.rb +46 -0
- data/lib/action_cable/connection/internal_channel.rb +45 -0
- data/lib/action_cable/connection/message_buffer.rb +54 -0
- data/lib/action_cable/connection/subscriptions.rb +76 -0
- data/lib/action_cable/connection/tagged_logger_proxy.rb +40 -0
- data/lib/action_cable/connection/web_socket.rb +29 -0
- data/lib/action_cable/engine.rb +38 -0
- data/lib/action_cable/gem_version.rb +15 -0
- data/lib/action_cable/helpers/action_cable_helper.rb +29 -0
- data/lib/action_cable/process/logging.rb +10 -0
- data/lib/action_cable/remote_connections.rb +64 -0
- data/lib/action_cable/server.rb +19 -0
- data/lib/action_cable/server/base.rb +77 -0
- data/lib/action_cable/server/broadcasting.rb +54 -0
- data/lib/action_cable/server/configuration.rb +35 -0
- data/lib/action_cable/server/connections.rb +37 -0
- data/lib/action_cable/server/worker.rb +42 -0
- data/lib/action_cable/server/worker/active_record_connection_management.rb +22 -0
- data/lib/action_cable/version.rb +6 -1
- data/lib/assets/javascripts/action_cable.coffee.erb +23 -0
- data/lib/assets/javascripts/action_cable/connection.coffee +84 -0
- data/lib/assets/javascripts/action_cable/connection_monitor.coffee +84 -0
- data/lib/assets/javascripts/action_cable/consumer.coffee +31 -0
- data/lib/assets/javascripts/action_cable/subscription.coffee +68 -0
- data/lib/assets/javascripts/action_cable/subscriptions.coffee +78 -0
- data/lib/rails/generators/channel/USAGE +14 -0
- data/lib/rails/generators/channel/channel_generator.rb +21 -0
- data/lib/rails/generators/channel/templates/assets/channel.coffee +14 -0
- data/lib/rails/generators/channel/templates/channel.rb +17 -0
- metadata +161 -26
- data/.gitignore +0 -9
- data/Gemfile +0 -4
- data/LICENSE.txt +0 -21
- data/Rakefile +0 -2
- data/actioncable.gemspec +0 -22
- data/bin/console +0 -14
- data/bin/setup +0 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a1ba4b27764e87b52a2d8d62e14be934f784d37a
|
4
|
+
data.tar.gz: 33c469154b8f384fa7ef5793b6d3ad4c7fed112d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7a69e1d0ee32f1101098d7b4fa9460a941af39cefcd243a28bdbfae65dcfa86c010d1f67447d89ee074e2d2a980b984a301da5743a2231357caa0efc852fef1b
|
7
|
+
data.tar.gz: fc5a28499b48d98bb39d31732421fc1a76c904b792ce3b8f39210adde803979416ecf1eed2a3b0e5fd10cbbd1620bcba29d9a41a0218dc156255dc0d55ea7cdd
|
data/CHANGELOG.md
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2015 Basecamp, LLC
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
CHANGED
@@ -1,39 +1,457 @@
|
|
1
|
-
#
|
1
|
+
# Action Cable –- Integrated WebSockets for Rails
|
2
2
|
|
3
|
-
|
3
|
+
Action Cable seamlessly integrates WebSockets with the rest of your Rails application.
|
4
|
+
It allows for real-time features to be written in Ruby in the same style
|
5
|
+
and form as the rest of your Rails application, while still being performant
|
6
|
+
and scalable. It's a full-stack offering that provides both a client-side
|
7
|
+
JavaScript framework and a server-side Ruby framework. You have access to your full
|
8
|
+
domain model written with Active Record or your ORM of choice.
|
4
9
|
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
6
10
|
|
7
|
-
##
|
11
|
+
## Terminology
|
8
12
|
|
9
|
-
|
13
|
+
A single Action Cable server can handle multiple connection instances. It has one
|
14
|
+
connection instance per WebSocket connection. A single user may have multiple
|
15
|
+
WebSockets open to your application if they use multiple browser tabs or devices.
|
16
|
+
The client of a WebSocket connection is called the consumer.
|
17
|
+
|
18
|
+
Each consumer can in turn subscribe to multiple cable channels. Each channel encapsulates
|
19
|
+
a logical unit of work, similar to what a controller does in a regular MVC setup. For example,
|
20
|
+
you could have a `ChatChannel` and a `AppearancesChannel`, and a consumer could be subscribed to either
|
21
|
+
or to both of these channels. At the very least, a consumer should be subscribed to one channel.
|
22
|
+
|
23
|
+
When the consumer is subscribed to a channel, they act as a subscriber. The connection between
|
24
|
+
the subscriber and the channel is, surprise-surprise, called a subscription. A consumer
|
25
|
+
can act as a subscriber to a given channel any number of times. For example, a consumer
|
26
|
+
could subscribe to multiple chat rooms at the same time. (And remember that a physical user may
|
27
|
+
have multiple consumers, one per tab/device open to your connection).
|
28
|
+
|
29
|
+
Each channel can then again be streaming zero or more broadcastings. A broadcasting is a
|
30
|
+
pubsub link where anything transmitted by the broadcaster is sent directly to the channel
|
31
|
+
subscribers who are streaming that named broadcasting.
|
32
|
+
|
33
|
+
As you can see, this is a fairly deep architectural stack. There's a lot of new terminology
|
34
|
+
to identify the new pieces, and on top of that, you're dealing with both client and server side
|
35
|
+
reflections of each unit.
|
36
|
+
|
37
|
+
## Examples
|
38
|
+
|
39
|
+
### A full-stack example
|
40
|
+
|
41
|
+
The first thing you must do is define your `ApplicationCable::Connection` class in Ruby. This
|
42
|
+
is the place where you authorize the incoming connection, and proceed to establish it
|
43
|
+
if all is well. Here's the simplest example starting with the server-side connection class:
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
# app/channels/application_cable/connection.rb
|
47
|
+
module ApplicationCable
|
48
|
+
class Connection < ActionCable::Connection::Base
|
49
|
+
identified_by :current_user
|
50
|
+
|
51
|
+
def connect
|
52
|
+
self.current_user = find_verified_user
|
53
|
+
end
|
54
|
+
|
55
|
+
protected
|
56
|
+
def find_verified_user
|
57
|
+
if current_user = User.find_by(id: cookies.signed[:user_id])
|
58
|
+
current_user
|
59
|
+
else
|
60
|
+
reject_unauthorized_connection
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
```
|
66
|
+
Here `identified_by` is a connection identifier that can be used to find the specific connection again or later.
|
67
|
+
Note that anything marked as an identifier will automatically create a delegate by the same name on any channel instances created off the connection.
|
68
|
+
|
69
|
+
Then you should define your `ApplicationCable::Channel` class in Ruby. This is the place where you put
|
70
|
+
shared logic between your channels.
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
# app/channels/application_cable/channel.rb
|
74
|
+
module ApplicationCable
|
75
|
+
class Channel < ActionCable::Channel::Base
|
76
|
+
end
|
77
|
+
end
|
78
|
+
```
|
79
|
+
|
80
|
+
This relies on the fact that you will already have handled authentication of the user, and
|
81
|
+
that a successful authentication sets a signed cookie with the `user_id`. This cookie is then
|
82
|
+
automatically sent to the connection instance when a new connection is attempted, and you
|
83
|
+
use that to set the `current_user`. By identifying the connection by this same current_user,
|
84
|
+
you're also ensuring that you can later retrieve all open connections by a given user (and
|
85
|
+
potentially disconnect them all if the user is deleted or deauthorized).
|
86
|
+
|
87
|
+
The client-side needs to setup a consumer instance of this connection. That's done like so:
|
88
|
+
|
89
|
+
```coffeescript
|
90
|
+
# app/assets/javascripts/cable.coffee
|
91
|
+
#= require action_cable
|
92
|
+
|
93
|
+
@App = {}
|
94
|
+
App.cable = Cable.createConsumer("ws://cable.example.com")
|
95
|
+
```
|
96
|
+
|
97
|
+
The ws://cable.example.com address must point to your set of Action Cable servers, and it
|
98
|
+
must share a cookie namespace with the rest of the application (which may live under http://example.com).
|
99
|
+
This ensures that the signed cookie will be correctly sent.
|
100
|
+
|
101
|
+
That's all you need to establish the connection! But of course, this isn't very useful in
|
102
|
+
itself. This just gives you the plumbing. To make stuff happen, you need content. That content
|
103
|
+
is defined by declaring channels on the server and allowing the consumer to subscribe to them.
|
104
|
+
|
105
|
+
|
106
|
+
### Channel example 1: User appearances
|
107
|
+
|
108
|
+
Here's a simple example of a channel that tracks whether a user is online or not and what page they're on.
|
109
|
+
(This is useful for creating presence features like showing a green dot next to a user name if they're online).
|
110
|
+
|
111
|
+
First you declare the server-side channel:
|
10
112
|
|
11
113
|
```ruby
|
12
|
-
|
114
|
+
# app/channels/appearance_channel.rb
|
115
|
+
class AppearanceChannel < ApplicationCable::Channel
|
116
|
+
def subscribed
|
117
|
+
current_user.appear
|
118
|
+
end
|
119
|
+
|
120
|
+
def unsubscribed
|
121
|
+
current_user.disappear
|
122
|
+
end
|
123
|
+
|
124
|
+
def appear(data)
|
125
|
+
current_user.appear on: data['appearing_on']
|
126
|
+
end
|
127
|
+
|
128
|
+
def away
|
129
|
+
current_user.away
|
130
|
+
end
|
131
|
+
end
|
13
132
|
```
|
14
133
|
|
15
|
-
|
134
|
+
The `#subscribed` callback is invoked when, as we'll show below, a client-side subscription is initiated. In this case,
|
135
|
+
we take that opportunity to say "the current user has indeed appeared". That appear/disappear API could be backed by
|
136
|
+
Redis or a database or whatever else. Here's what the client-side of that looks like:
|
137
|
+
|
138
|
+
```coffeescript
|
139
|
+
# app/assets/javascripts/cable/subscriptions/appearance.coffee
|
140
|
+
App.cable.subscriptions.create "AppearanceChannel",
|
141
|
+
# Called when the subscription is ready for use on the server
|
142
|
+
connected: ->
|
143
|
+
@install()
|
144
|
+
@appear()
|
145
|
+
|
146
|
+
# Called when the WebSocket connection is closed
|
147
|
+
disconnected: ->
|
148
|
+
@uninstall()
|
149
|
+
|
150
|
+
# Called when the subscription is rejected by the server
|
151
|
+
rejected: ->
|
152
|
+
@uninstall()
|
153
|
+
|
154
|
+
appear: ->
|
155
|
+
# Calls `AppearanceChannel#appear(data)` on the server
|
156
|
+
@perform("appear", appearing_on: $("main").data("appearing-on"))
|
157
|
+
|
158
|
+
away: ->
|
159
|
+
# Calls `AppearanceChannel#away` on the server
|
160
|
+
@perform("away")
|
161
|
+
|
162
|
+
|
163
|
+
buttonSelector = "[data-behavior~=appear_away]"
|
164
|
+
|
165
|
+
install: ->
|
166
|
+
$(document).on "page:change.appearance", =>
|
167
|
+
@appear()
|
168
|
+
|
169
|
+
$(document).on "click.appearance", buttonSelector, =>
|
170
|
+
@away()
|
171
|
+
false
|
172
|
+
|
173
|
+
$(buttonSelector).show()
|
174
|
+
|
175
|
+
uninstall: ->
|
176
|
+
$(document).off(".appearance")
|
177
|
+
$(buttonSelector).hide()
|
178
|
+
```
|
179
|
+
|
180
|
+
Simply calling `App.cable.subscriptions.create` will setup the subscription, which will call `AppearanceChannel#subscribed`,
|
181
|
+
which in turn is linked to original `App.cable` -> `ApplicationCable::Connection` instances.
|
182
|
+
|
183
|
+
We then link the client-side `appear` method to `AppearanceChannel#appear(data)`. This is possible because the server-side
|
184
|
+
channel instance will automatically expose the public methods declared on the class (minus the callbacks), so that these
|
185
|
+
can be reached as remote procedure calls via a subscription's `perform` method.
|
186
|
+
|
187
|
+
### Channel example 2: Receiving new web notifications
|
188
|
+
|
189
|
+
The appearance example was all about exposing server functionality to client-side invocation over the WebSocket connection.
|
190
|
+
But the great thing about WebSockets is that it's a two-way street. So now let's show an example where the server invokes
|
191
|
+
action on the client.
|
192
|
+
|
193
|
+
This is a web notification channel that allows you to trigger client-side web notifications when you broadcast to the right
|
194
|
+
streams:
|
195
|
+
|
196
|
+
```ruby
|
197
|
+
# app/channels/web_notifications_channel.rb
|
198
|
+
class WebNotificationsChannel < ApplicationCable::Channel
|
199
|
+
def subscribed
|
200
|
+
stream_from "web_notifications_#{current_user.id}"
|
201
|
+
end
|
202
|
+
end
|
203
|
+
```
|
204
|
+
|
205
|
+
```coffeescript
|
206
|
+
# Client-side, which assumes you've already requested the right to send web notifications
|
207
|
+
App.cable.subscriptions.create "WebNotificationsChannel",
|
208
|
+
received: (data) ->
|
209
|
+
new Notification data["title"], body: data["body"]
|
210
|
+
```
|
211
|
+
|
212
|
+
```ruby
|
213
|
+
# Somewhere in your app this is called, perhaps from a NewCommentJob
|
214
|
+
ActionCable.server.broadcast \
|
215
|
+
"web_notifications_#{current_user.id}", { title: 'New things!', body: 'All the news that is fit to print' }
|
216
|
+
```
|
217
|
+
|
218
|
+
The `ActionCable.server.broadcast` call places a message in the Redis' pubsub queue under a separate broadcasting name for each user. For a user with an ID of 1, the broadcasting name would be `web_notifications_1`.
|
219
|
+
The channel has been instructed to stream everything that arrives at `web_notifications_1` directly to the client by invoking the
|
220
|
+
`#received(data)` callback. The data is the hash sent as the second parameter to the server-side broadcast call, JSON encoded for the trip
|
221
|
+
across the wire, and unpacked for the data argument arriving to `#received`.
|
222
|
+
|
223
|
+
|
224
|
+
### Passing Parameters to Channel
|
225
|
+
|
226
|
+
You can pass parameters from the client side to the server side when creating a subscription. For example:
|
227
|
+
|
228
|
+
```ruby
|
229
|
+
# app/channels/chat_channel.rb
|
230
|
+
class ChatChannel < ApplicationCable::Channel
|
231
|
+
def subscribed
|
232
|
+
stream_from "chat_#{params[:room]}"
|
233
|
+
end
|
234
|
+
end
|
235
|
+
```
|
236
|
+
|
237
|
+
Pass an object as the first argument to `subscriptions.create`, and that object will become your params hash in your cable channel. The keyword `channel` is required.
|
238
|
+
|
239
|
+
```coffeescript
|
240
|
+
# Client-side, which assumes you've already requested the right to send web notifications
|
241
|
+
App.cable.subscriptions.create { channel: "ChatChannel", room: "Best Room" },
|
242
|
+
received: (data) ->
|
243
|
+
@appendLine(data)
|
244
|
+
|
245
|
+
appendLine: (data) ->
|
246
|
+
html = @createLine(data)
|
247
|
+
$("[data-chat-room='Best Room']").append(html)
|
248
|
+
|
249
|
+
createLine: (data) ->
|
250
|
+
"""
|
251
|
+
<article class="chat-line">
|
252
|
+
<span class="speaker">#{data["sent_by"]}</span>
|
253
|
+
<span class="body">#{data["body"]}</span>
|
254
|
+
</article>
|
255
|
+
"""
|
256
|
+
```
|
257
|
+
|
258
|
+
```ruby
|
259
|
+
# Somewhere in your app this is called, perhaps from a NewCommentJob
|
260
|
+
ActionCable.server.broadcast \
|
261
|
+
"chat_#{room}", { sent_by: 'Paul', body: 'This is a cool chat app.' }
|
262
|
+
```
|
263
|
+
|
264
|
+
|
265
|
+
### Rebroadcasting message
|
266
|
+
|
267
|
+
A common use case is to rebroadcast a message sent by one client to any other connected clients.
|
268
|
+
|
269
|
+
```ruby
|
270
|
+
# app/channels/chat_channel.rb
|
271
|
+
class ChatChannel < ApplicationCable::Channel
|
272
|
+
def subscribed
|
273
|
+
stream_from "chat_#{params[:room]}"
|
274
|
+
end
|
275
|
+
|
276
|
+
def receive(data)
|
277
|
+
ActionCable.server.broadcast "chat_#{params[:room]}", data
|
278
|
+
end
|
279
|
+
end
|
280
|
+
```
|
281
|
+
|
282
|
+
```coffeescript
|
283
|
+
# Client-side, which assumes you've already requested the right to send web notifications
|
284
|
+
App.chatChannel = App.cable.subscriptions.create { channel: "ChatChannel", room: "Best Room" },
|
285
|
+
received: (data) ->
|
286
|
+
# data => { sent_by: "Paul", body: "This is a cool chat app." }
|
287
|
+
|
288
|
+
App.chatChannel.send({ sent_by: "Paul", body: "This is a cool chat app." })
|
289
|
+
```
|
290
|
+
|
291
|
+
The rebroadcast will be received by all connected clients, _including_ the client that sent the message. Note that params are the same as they were when you subscribed to the channel.
|
292
|
+
|
293
|
+
|
294
|
+
### More complete examples
|
295
|
+
|
296
|
+
See the [rails/actioncable-examples](http://github.com/rails/actioncable-examples) repository for a full example of how to setup Action Cable in a Rails app and adding channels.
|
297
|
+
|
298
|
+
|
299
|
+
## Configuration
|
300
|
+
|
301
|
+
Action Cable has two required configurations: the Redis connection and specifying allowed request origins.
|
302
|
+
|
303
|
+
### Redis
|
304
|
+
|
305
|
+
By default, `ActionCable::Server::Base` will look for a configuration file in `Rails.root.join('config/redis/cable.yml')`. The file must follow the following format:
|
306
|
+
|
307
|
+
```yaml
|
308
|
+
production: &production
|
309
|
+
url: redis://10.10.3.153:6381
|
310
|
+
development: &development
|
311
|
+
url: redis://localhost:6379
|
312
|
+
test: *development
|
313
|
+
```
|
314
|
+
|
315
|
+
This format allows you to specify one configuration per Rails environment. You can also change the location of the Redis config file in
|
316
|
+
a Rails initializer with something like:
|
317
|
+
|
318
|
+
```ruby
|
319
|
+
Rails.application.paths.add "config/redis/cable", with: "somewhere/else/cable.yml"
|
320
|
+
```
|
321
|
+
|
322
|
+
### Allowed Request Origins
|
323
|
+
|
324
|
+
Action Cable will only accept requests from specified origins, which are passed to the server config as an array. The origins can be instances of strings or regular expressions, against which a check for match will be performed.
|
325
|
+
|
326
|
+
```ruby
|
327
|
+
ActionCable.server.config.allowed_request_origins = ['http://rubyonrails.com', /http:\/\/ruby.*/]
|
328
|
+
```
|
329
|
+
|
330
|
+
To disable and allow requests from any origin:
|
331
|
+
|
332
|
+
```ruby
|
333
|
+
ActionCable.server.config.disable_request_forgery_protection = true
|
334
|
+
```
|
335
|
+
|
336
|
+
By default, Action Cable allows all requests from localhost:3000 when running in the development environment.
|
337
|
+
|
338
|
+
### Other Configurations
|
339
|
+
|
340
|
+
The other common option to configure is the log tags applied to the per-connection logger. Here's close to what we're using in Basecamp:
|
341
|
+
|
342
|
+
```ruby
|
343
|
+
ActionCable.server.config.log_tags = [
|
344
|
+
-> request { request.env['bc.account_id'] || "no-account" },
|
345
|
+
:action_cable,
|
346
|
+
-> request { request.uuid }
|
347
|
+
]
|
348
|
+
```
|
349
|
+
|
350
|
+
Your websocket url might change between environments. If you host your production server via https, you will need to use the wss scheme
|
351
|
+
for your ActionCable server, but development might remain http and use the ws scheme. You might use localhost in development and your
|
352
|
+
domain in production. In any case, to vary the websocket url between environments, add the following configuration to each environment:
|
353
|
+
|
354
|
+
```ruby
|
355
|
+
config.action_cable.url = "ws://example.com:28080"
|
356
|
+
```
|
357
|
+
|
358
|
+
Then add the following line to your layout before your JavaScript tag:
|
359
|
+
|
360
|
+
```erb
|
361
|
+
<%= action_cable_meta_tag %>
|
362
|
+
```
|
363
|
+
|
364
|
+
And finally, create your consumer like so:
|
365
|
+
|
366
|
+
```coffeescript
|
367
|
+
App.cable = Cable.createConsumer()
|
368
|
+
```
|
369
|
+
|
370
|
+
For a full list of all configuration options, see the `ActionCable::Server::Configuration` class.
|
371
|
+
|
372
|
+
Also note that your server must provide at least the same number of database connections as you have workers. The default worker pool is set to 100, so that means you have to make at least that available. You can change that in `config/database.yml` through the `pool` attribute.
|
373
|
+
|
374
|
+
|
375
|
+
## Running the cable server
|
376
|
+
|
377
|
+
### Standalone
|
378
|
+
The cable server(s) is separated from your normal application server. It's still a rack application, but it is its own rack
|
379
|
+
application. The recommended basic setup is as follows:
|
380
|
+
|
381
|
+
```ruby
|
382
|
+
# cable/config.ru
|
383
|
+
require ::File.expand_path('../../config/environment', __FILE__)
|
384
|
+
Rails.application.eager_load!
|
385
|
+
|
386
|
+
require 'action_cable/process/logging'
|
387
|
+
|
388
|
+
run ActionCable.server
|
389
|
+
```
|
390
|
+
|
391
|
+
Then you start the server using a binstub in bin/cable ala:
|
392
|
+
```
|
393
|
+
#!/bin/bash
|
394
|
+
bundle exec puma -p 28080 cable/config.ru
|
395
|
+
```
|
396
|
+
|
397
|
+
The above will start a cable server on port 28080. Remember to point your client-side setup against that using something like:
|
398
|
+
`App.cable = Cable.createConsumer("ws://basecamp.dev:28080")`.
|
399
|
+
|
400
|
+
### In app
|
401
|
+
|
402
|
+
If you are using a threaded server like Puma or Thin, the current implementation of ActionCable can run side-along with your Rails application. For example, to listen for WebSocket requests on `/websocket`, match requests on that path:
|
403
|
+
|
404
|
+
```ruby
|
405
|
+
# config/routes.rb
|
406
|
+
Example::Application.routes.draw do
|
407
|
+
match "/websocket", :to => ActionCable.server, via: [:get, :post]
|
408
|
+
end
|
409
|
+
```
|
410
|
+
|
411
|
+
You can use `App.cable = Cable.createConsumer("/websocket")` to connect to the cable server.
|
412
|
+
|
413
|
+
For every instance of your server you create and for every worker your server spawns, you will also have a new instance of ActionCable, but the use of Redis keeps messages synced across connections.
|
414
|
+
|
415
|
+
### Notes
|
416
|
+
|
417
|
+
Beware that currently the cable server will _not_ auto-reload any changes in the framework. As we've discussed, long-running cable connections mean long-running objects. We don't yet have a way of reloading the classes of those objects in a safe manner. So when you change your channels, or the model your channels use, you must restart the cable server.
|
418
|
+
|
419
|
+
We'll get all this abstracted properly when the framework is integrated into Rails.
|
420
|
+
|
421
|
+
The WebSocket server doesn't have access to the session, but it has access to the cookies. This can be used when you need to handle authentication. You can see one way of doing that with Devise in this [article](http://www.rubytutorial.io/actioncable-devise-authentication).
|
422
|
+
|
423
|
+
## Dependencies
|
424
|
+
|
425
|
+
Action Cable is currently tied to Redis through its use of the pubsub feature to route
|
426
|
+
messages back and forth over the WebSocket cable connection. This dependency may well
|
427
|
+
be alleviated in the future, but for the moment that's what it is. So be sure to have
|
428
|
+
Redis installed and running.
|
429
|
+
|
430
|
+
The Ruby side of things is built on top of [faye-websocket](https://github.com/faye/faye-websocket-ruby) and [celluloid](https://github.com/celluloid/celluloid).
|
431
|
+
|
16
432
|
|
17
|
-
|
433
|
+
## Deployment
|
18
434
|
|
19
|
-
|
435
|
+
Action Cable is powered by a combination of EventMachine and threads. The
|
436
|
+
framework plumbing needed for connection handling is handled in the
|
437
|
+
EventMachine loop, but the actual channel, user-specified, work is handled
|
438
|
+
in a normal Ruby thread. This means you can use all your regular Rails models
|
439
|
+
with no problem, as long as you haven't committed any thread-safety sins.
|
20
440
|
|
21
|
-
|
441
|
+
But this also means that Action Cable needs to run in its own server process.
|
442
|
+
So you'll have one set of server processes for your normal web work, and another
|
443
|
+
set of server processes for the Action Cable. The former can be single-threaded,
|
444
|
+
like Unicorn, but the latter must be multi-threaded, like Puma.
|
22
445
|
|
23
|
-
##
|
446
|
+
## License
|
24
447
|
|
25
|
-
|
448
|
+
Action Cable is released under the MIT license:
|
26
449
|
|
27
|
-
|
450
|
+
* http://www.opensource.org/licenses/MIT
|
28
451
|
|
29
|
-
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
452
|
|
31
|
-
|
453
|
+
## Support
|
32
454
|
|
33
|
-
|
455
|
+
Bug reports can be filed for the alpha development project here:
|
34
456
|
|
35
|
-
|
36
|
-
2. Create your feature branch (`git checkout -b my-new-feature`)
|
37
|
-
3. Commit your changes (`git commit -am 'Add some feature'`)
|
38
|
-
4. Push to the branch (`git push origin my-new-feature`)
|
39
|
-
5. Create a new Pull Request
|
457
|
+
* https://github.com/rails/actioncable/issues
|