mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
[ci skip] Several ActionCable documentation updates:
* Properly indent code sample in ActionCable::Channel::Streams * Add a doc comment for #stop_all_streams * Reformat + add <tt> blocks around code references in ActionCable::Base docs * Clarify and a little better grammar on ActionCable::RemoteConnections * Correct indentation and clean up ActionCable::Server::Broadcasting code sample
This commit is contained in:
parent
473f63734a
commit
8305437385
4 changed files with 53 additions and 40 deletions
|
@ -32,8 +32,11 @@ module ActionCable
|
||||||
#
|
#
|
||||||
# == Action processing
|
# == Action processing
|
||||||
#
|
#
|
||||||
# Unlike Action Controllers, channels do not follow a REST constraint form for its actions. It's a remote-procedure call model. You can
|
# Unlike subclasses of ActionController::Base, channels do not follow a REST
|
||||||
# declare any public method on the channel (optionally taking a data argument), and this method is automatically exposed as callable to the client.
|
# constraint form for their actions. Instead, ActionCable operates through a
|
||||||
|
# remote-procedure call model. You can declare any public method on the
|
||||||
|
# channel (optionally taking a <tt>data</tt> argument), and this method is
|
||||||
|
# automatically exposed as callable to the client.
|
||||||
#
|
#
|
||||||
# Example:
|
# Example:
|
||||||
#
|
#
|
||||||
|
@ -60,18 +63,22 @@ module ActionCable
|
||||||
# end
|
# end
|
||||||
# end
|
# end
|
||||||
#
|
#
|
||||||
# In this example, subscribed/unsubscribed are not callable methods, as they were already declared in ActionCable::Channel::Base, but #appear/away
|
# In this example, subscribed/unsubscribed are not callable methods, as they
|
||||||
# are. #generate_connection_token is also not callable as its a private method. You'll see that appear accepts a data parameter, which it then
|
# were already declared in ActionCable::Channel::Base, but <tt>#appear</tt>
|
||||||
# uses as part of its model call. #away does not, it's simply a trigger action.
|
# and <tt>#away</tt> are. <tt>#generate_connection_token</tt> is also not
|
||||||
|
# callable as it's a private method. You'll see that appear accepts a data
|
||||||
|
# parameter, which it then uses as part of its model call. <tt>#away</tt>
|
||||||
|
# does not, since it's simply a trigger action.
|
||||||
#
|
#
|
||||||
# Also note that in this example, current_user is available because it was marked as an identifying attribute on the connection.
|
# Also note that in this example, <tt>current_user</tt> is available because
|
||||||
# All such identifiers will automatically create a delegation method of the same name on the channel instance.
|
# it was marked as an identifying attribute on the connection. All such
|
||||||
|
# identifiers will automatically create a delegation method of the same name
|
||||||
|
# on the channel instance.
|
||||||
#
|
#
|
||||||
# == Rejecting subscription requests
|
# == Rejecting subscription requests
|
||||||
#
|
#
|
||||||
# A channel can reject a subscription request in the #subscribed callback by invoking #reject!
|
# A channel can reject a subscription request in the #subscribed callback by
|
||||||
#
|
# invoking the #reject method:
|
||||||
# Example:
|
|
||||||
#
|
#
|
||||||
# class ChatChannel < ApplicationCable::Channel
|
# class ChatChannel < ApplicationCable::Channel
|
||||||
# def subscribed
|
# def subscribed
|
||||||
|
@ -80,8 +87,10 @@ module ActionCable
|
||||||
# end
|
# end
|
||||||
# end
|
# end
|
||||||
#
|
#
|
||||||
# In this example, the subscription will be rejected if the current_user does not have access to the chat room.
|
# In this example, the subscription will be rejected if the
|
||||||
# On the client-side, Channel#rejected callback will get invoked when the server rejects the subscription request.
|
# <tt>current_user</tt> does not have access to the chat room. On the
|
||||||
|
# client-side, the <tt>Channel#rejected</tt> callback will get invoked when
|
||||||
|
# the server rejects the subscription request.
|
||||||
class Base
|
class Base
|
||||||
include Callbacks
|
include Callbacks
|
||||||
include PeriodicTimers
|
include PeriodicTimers
|
||||||
|
|
|
@ -57,6 +57,7 @@ module ActionCable
|
||||||
# transmit message
|
# transmit message
|
||||||
# end
|
# end
|
||||||
# end
|
# end
|
||||||
|
# end
|
||||||
#
|
#
|
||||||
# You can stop streaming from all broadcasts by calling #stop_all_streams.
|
# You can stop streaming from all broadcasts by calling #stop_all_streams.
|
||||||
module Streams
|
module Streams
|
||||||
|
@ -90,6 +91,7 @@ module ActionCable
|
||||||
stream_from(broadcasting_for([ channel_name, model ]), callback)
|
stream_from(broadcasting_for([ channel_name, model ]), callback)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Unsubscribes all streams associated with this channel from the pubsub queue.
|
||||||
def stop_all_streams
|
def stop_all_streams
|
||||||
streams.each do |broadcasting, callback|
|
streams.each do |broadcasting, callback|
|
||||||
pubsub.unsubscribe broadcasting, callback
|
pubsub.unsubscribe broadcasting, callback
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
module ActionCable
|
module ActionCable
|
||||||
# If you need to disconnect a given connection, you go through the RemoteConnections. You find the connections you're looking for by
|
# If you need to disconnect a given connection, you can go through the
|
||||||
# searching the identifier declared on the connection. Example:
|
# RemoteConnections. You can find the connections you're looking for by
|
||||||
|
# searching for the identifier declared on the connection. For example:
|
||||||
#
|
#
|
||||||
# module ApplicationCable
|
# module ApplicationCable
|
||||||
# class Connection < ActionCable::Connection::Base
|
# class Connection < ActionCable::Connection::Base
|
||||||
|
@ -11,8 +12,9 @@ module ActionCable
|
||||||
#
|
#
|
||||||
# ActionCable.server.remote_connections.where(current_user: User.find(1)).disconnect
|
# ActionCable.server.remote_connections.where(current_user: User.find(1)).disconnect
|
||||||
#
|
#
|
||||||
# That will disconnect all the connections established for User.find(1) across all servers running on all machines (because it uses
|
# This will disconnect all the connections established for
|
||||||
# the internal channel that all these servers are subscribed to).
|
# <tt>User.find(1)</tt> across all servers running on all machines, because
|
||||||
|
# it uses the internal channel that all these servers are subscribed to.
|
||||||
class RemoteConnections
|
class RemoteConnections
|
||||||
attr_reader :server
|
attr_reader :server
|
||||||
|
|
||||||
|
@ -25,7 +27,7 @@ module ActionCable
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
# Represents a single remote connection found via ActionCable.server.remote_connections.where(*).
|
# Represents a single remote connection found via <tt>ActionCable.server.remote_connections.where(*)</tt>.
|
||||||
# Exists for the solely for the purpose of calling #disconnect on that connection.
|
# Exists for the solely for the purpose of calling #disconnect on that connection.
|
||||||
class RemoteConnection
|
class RemoteConnection
|
||||||
class InvalidIdentifiersError < StandardError; end
|
class InvalidIdentifiersError < StandardError; end
|
||||||
|
|
|
@ -11,9 +11,9 @@ module ActionCable
|
||||||
#
|
#
|
||||||
# # Somewhere in your app this is called, perhaps from a NewCommentJob
|
# # Somewhere in your app this is called, perhaps from a NewCommentJob
|
||||||
# ActionCable.server.broadcast \
|
# ActionCable.server.broadcast \
|
||||||
# "web_notifications_1", { title: 'New things!', body: 'All shit fit for print' }
|
# "web_notifications_1", { title: "New things!", body: "All that's fit for print" }
|
||||||
#
|
#
|
||||||
# # Client-side coffescript, which assumes you've already requested the right to send web notifications
|
# # Client-side CoffeeScript, which assumes you've already requested the right to send web notifications
|
||||||
# App.cable.subscriptions.create "WebNotificationsChannel",
|
# App.cable.subscriptions.create "WebNotificationsChannel",
|
||||||
# received: (data) ->
|
# received: (data) ->
|
||||||
# new Notification data['title'], body: data['body']
|
# new Notification data['title'], body: data['body']
|
||||||
|
|
Loading…
Reference in a new issue