@@ -35,13 +35,13 @@ client-server connection instance established per WebSocket connection.
35
35
## Server-Side Components
36
36
37
37
### Connections
38
- Connections form the foundaton of the client-server relationship. For every WebSocket
38
+ Connections form the foundation of the client-server relationship. For every WebSocket
39
39
the cable server is accepting, a Connection object will be instantiated. This instance
40
- becomes the parent of all the channel subscriptions that are created from there on.
40
+ becomes the parent of all the channel subscriptions that are created from there on.
41
41
The Connection itself does not deal with any specific application logic beyond authentication
42
- and authorization. The client of a WebSocket connection is called the consumer.
42
+ and authorization. The client of a WebSocket connection is called the consumer.
43
43
A single consumer may have multiple WebSockets open to your application if they
44
- use multiple browser tabs or devices.
44
+ use multiple browser tabs or devices.
45
45
46
46
Connections are instantiated via the ` ApplicationCable::Connection ` class in Ruby.
47
47
In this class, you authorize the incoming connection, and proceed to establish it
@@ -75,7 +75,7 @@ Note that anything marked as an identifier will automatically create a delegate
75
75
by the same name on any channel instances created off the connection.
76
76
77
77
This relies on the fact that you will already have handled authentication of the user,
78
- and that a successful authentication sets a signed cookie with the ` user_id ` .
78
+ and that a successful authentication sets a signed cookie with the ` user_id ` .
79
79
This cookie is then automatically sent to the connection instance when a new connection
80
80
is attempted, and you use that to set the ` current_user ` . By identifying the connection
81
81
by this same current_user, you're also ensuring that you can later retrieve all open
@@ -84,9 +84,9 @@ or deauthorized).
84
84
85
85
### Channels
86
86
A channel encapsulates a logical unit of work, similar to what a controller does in a
87
- regular MVC setup.
87
+ regular MVC setup.
88
88
By default, Rails creates a parent ` ApplicationCable::Channel ` class for encapsulating
89
- shared logic between your channels.
89
+ shared logic between your channels.
90
90
91
91
#### Parent Channel Setup
92
92
``` ruby
@@ -145,11 +145,11 @@ This ensures that the signed cookie will be correctly sent.
145
145
146
146
#### Subscriber
147
147
When a consumer is subscribed to a channel, they act as a subscriber. A
148
- consumer can act as a subscriber to a given channel any number of times.
149
- For example, a consumer could subscribe to multiple chat rooms at the same time.
148
+ consumer can act as a subscriber to a given channel any number of times.
149
+ For example, a consumer could subscribe to multiple chat rooms at the same time.
150
150
(remember that a physical user may have multiple consumers, one per tab/device open to your connection).
151
151
152
- A consumer becomes a subscriber, by creating a subscribtion to a given channel:
152
+ A consumer becomes a subscriber, by creating a subscription to a given channel:
153
153
``` coffeescript
154
154
# app/assets/javascripts/cable/subscriptions/chat.coffee
155
155
# Assumes you've already requested the right to send web notifications
197
197
198
198
A broadcasting is a pub/sub link where anything transmitted by a publisher
199
199
is routed directly to the channel subscribers who are streaming that named
200
- broadcasting. Each channel can be streaming zero or more broadcastings.
201
- Broadcastings are purely an online queue and time dependent;
200
+ broadcasting. Each channel can be streaming zero or more broadcastings.
201
+ Broadcastings are purely an online queue and time dependent;
202
202
If a consumer is not streaming (subscribed to a given channel), they'll not
203
203
get the broadcast should they connect later.
204
204
@@ -218,9 +218,9 @@ callback.
218
218
219
219
### Subscriptions
220
220
221
- When a consumer is subscribed to a channel, they act as a subscriber;
221
+ When a consumer is subscribed to a channel, they act as a subscriber;
222
222
This connection is called a subscription. Incoming messages are then routed
223
- to these channel subscriptions based on an identifier sent by the cable consumer.
223
+ to these channel subscriptions based on an identifier sent by the cable consumer.
224
224
225
225
``` coffeescript
226
226
# app/assets/javascripts/cable/subscriptions/chat.coffee
@@ -414,13 +414,13 @@ App.cable.subscriptions.create "AppearanceChannel",
414
414
The appearance example was all about exposing server functionality to
415
415
client-side invocation over the WebSocket connection. But the great thing
416
416
about WebSockets is that it's a two-way street. So now let's show an example
417
- where the server invokesan action on the client.
417
+ where the server invokes an action on the client.
418
418
419
419
This is a web notification channel that allows you to trigger client-side
420
420
web notifications when you broadcast to the right streams:
421
421
422
422
#### Create the server-side Web Notifications Channel:
423
-
423
+
424
424
``` ruby
425
425
# app/channels/web_notifications_channel.rb
426
426
class WebNotificationsChannel < ApplicationCable ::Channel
@@ -520,12 +520,12 @@ this would be something like: `App.cable = ActionCable.createConsumer("ws://exam
520
520
and for an in-app server, something like: ` App.cable = ActionCable.createConsumer("/cable") ` .
521
521
522
522
The second option is to pass the server url through the ` action_cable_meta_tag ` in your layout.
523
- This uses a url or path typically set via ` config.action_cable.url `
523
+ This uses a url or path typically set via ` config.action_cable.url `
524
524
in the environment configuration files, or defaults to "/cable".
525
525
526
526
This method is especially useful if your WebSocket url might change
527
527
between environments. If you host your production server via https,
528
- you will need to use the wss scheme for your ActionCable server, but
528
+ you will need to use the wss scheme for your ActionCable server, but
529
529
development might remain http and use the ws scheme. You might use localhost
530
530
in development and your domain in production.
531
531
@@ -606,15 +606,15 @@ end
606
606
607
607
You can use ` App.cable = ActionCable.createConsumer() ` to connect to the
608
608
cable server if ` action_cable_meta_tag ` is included in the layout. A custom
609
- path is specified as first argument to ` createConsumer `
609
+ path is specified as first argument to ` createConsumer `
610
610
(e.g. ` App.cable = ActionCable.createConsumer("/websocket") ` ).
611
611
612
612
For every instance of your server you create and for every worker
613
613
your server spawns, you will also have a new instance of ActionCable,
614
- but the use of Redis keeps messages synced across connections.
614
+ but the use of Redis keeps messages synced across connections.
615
615
616
616
### Standalone
617
- The cable server(s) can be separated from your normal application server.
617
+ The cable server(s) can be separated from your normal application server.
618
618
It's still a Rack application, but it is its own Rack application.
619
619
The recommended basic setup is as follows:
620
620
@@ -639,7 +639,7 @@ The above will start a cable server on port 28080.
639
639
Beware that currently the cable server will _ not_ auto-reload any
640
640
changes in the framework. As we've discussed, long-running cable
641
641
connections mean long-running objects. We don't yet have a way of
642
- reloading the classes of those objects in a safe manner. So when
642
+ reloading the classes of those objects in a safe manner. So when
643
643
you change your channels, or the model your channels use, you must
644
644
restart the cable server.
645
645
@@ -660,11 +660,11 @@ The Ruby side of things is built on top of [websocket-driver](https://github.com
660
660
## Deployment
661
661
662
662
Action Cable is powered by a combination of WebSockets and threads. Both the
663
- framework plumbing and user-specified channel work are handled internally by
664
- utilizing Ruby's native thread support. This means you can use all your regular
663
+ framework plumbing and user-specified channel work are handled internally by
664
+ utilizing Ruby's native thread support. This means you can use all your regular
665
665
Rails models with no problem, as long as you haven't committed any thread-safety sins.
666
666
667
- The Action Cable server implements the Rack socket hijacking API,
667
+ The Action Cable server implements the Rack socket hijacking API,
668
668
thereby allowing the use of a multithreaded pattern for managing connections
669
669
internally, irrespective of whether the application server is multi-threaded or not.
670
670
0 commit comments