Class: LiteCable::Channel::Base
- Inherits:
-
Object
- Object
- LiteCable::Channel::Base
- Defined in:
- lib/lite_cable/channel/base.rb
Overview
The channel provides the basic structure of grouping behavior into logical units when communicating over the connection. You can think of a channel like a form of controller, but one that’s capable of pushing content to the subscriber in addition to simply responding to the subscriber’s direct requests.
Identification
Each channel must have a unique identifier, which is used by the connection to resolve the channel’s class.
Example:
class SecretChannel < LiteCable::Channel::Base
identifier 'my_super_secret_channel'
end
# client-side
App.cable.subscriptions.create('my_super_secret_channel')
Action processing
You can declare any public method on the channel (optionally taking a ‘data` argument), and this method is automatically exposed as callable to the client.
Example:
class AppearanceChannel < LiteCable::Channel::Base
def unsubscribed
# here `current_user` is a connection identifier
current_user.disappear
end
def appear(data)
current_user.appear on: data['appearing_on']
end
def away
current_user.away
end
end
Rejecting subscription requests
A channel can reject a subscription request in the #subscribed callback by invoking the #reject method:
class ChatChannel < ApplicationCable::Channel
def subscribed
room = Chat::Room[params['room_number']]
reject unless current_user.can_access?(room)
end
end
In this example, the subscription will be rejected if the current_user
does not have access to the chat room. On the client-side, the Channel#rejected
callback will get invoked when the server rejects the subscription request.
Constant Summary
Constants included from Logging
Class Attribute Summary collapse
-
.id ⇒ Object
readonly
Returns the value of attribute id.
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#identifier ⇒ Object
readonly
Returns the value of attribute identifier.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
Class Method Summary collapse
-
.action_methods ⇒ Object
A set of method names that should be considered actions.
-
.identifier(id) ⇒ Object
Register the channel by its unique identifier (in order to resolve the channel’s class for connections).
Instance Method Summary collapse
- #handle_action(encoded_message) ⇒ Object
- #handle_subscribe ⇒ Object
- #handle_unsubscribe ⇒ Object
-
#initialize(connection, identifier, params) ⇒ Base
constructor
A new instance of Base.
Methods included from Logging
Methods included from Streams
#stop_all_streams, #stop_stream, #stream_from
Constructor Details
#initialize(connection, identifier, params) ⇒ Base
Returns a new instance of Base.
97 98 99 100 101 102 103 |
# File 'lib/lite_cable/channel/base.rb', line 97 def initialize(connection, identifier, params) @connection = connection @identifier = identifier @params = params.freeze delegate_connection_identifiers end |
Class Attribute Details
.id ⇒ Object (readonly)
Returns the value of attribute id.
82 83 84 |
# File 'lib/lite_cable/channel/base.rb', line 82 def id @id end |
Instance Attribute Details
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
95 96 97 |
# File 'lib/lite_cable/channel/base.rb', line 95 def connection @connection end |
#identifier ⇒ Object (readonly)
Returns the value of attribute identifier.
95 96 97 |
# File 'lib/lite_cable/channel/base.rb', line 95 def identifier @identifier end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
95 96 97 |
# File 'lib/lite_cable/channel/base.rb', line 95 def params @params end |
Class Method Details
.action_methods ⇒ Object
A set of method names that should be considered actions. This includes all public instance methods on a channel except from Channel::Base methods.
70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/lite_cable/channel/base.rb', line 70 def action_methods @action_methods ||= begin # All public instance methods of this class, including ancestors methods = (public_instance_methods(true) - # Except for public instance methods of Base and its ancestors LiteCable::Channel::Base.public_instance_methods(true) + # Be sure to include shadowed public instance methods of this class public_instance_methods(false)).uniq.map(&:to_s) methods.to_set end end |
Instance Method Details
#handle_action(encoded_message) ⇒ Object
113 114 115 |
# File 'lib/lite_cable/channel/base.rb', line 113 def handle_action() perform_action connection.coder.decode() end |
#handle_subscribe ⇒ Object
105 106 107 |
# File 'lib/lite_cable/channel/base.rb', line 105 def handle_subscribe subscribed if respond_to?(:subscribed) end |
#handle_unsubscribe ⇒ Object
109 110 111 |
# File 'lib/lite_cable/channel/base.rb', line 109 def handle_unsubscribe unsubscribed if respond_to?(:unsubscribed) end |