Class: LiteCable::Channel::Base

Inherits:
Object
  • Object
show all
Includes:
Streams, Logging
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

Logging::PREFIX

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

logger

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

.idObject (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

#connectionObject (readonly)

Returns the value of attribute connection.



95
96
97
# File 'lib/lite_cable/channel/base.rb', line 95

def connection
  @connection
end

#identifierObject (readonly)

Returns the value of attribute identifier.



95
96
97
# File 'lib/lite_cable/channel/base.rb', line 95

def identifier
  @identifier
end

#paramsObject (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_methodsObject

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

.identifier(id) ⇒ Object

Register the channel by its unique identifier (in order to resolve the channel’s class for connections)



86
87
88
89
# File 'lib/lite_cable/channel/base.rb', line 86

def identifier(id)
  Registry.add(id.to_s, self)
  @id = id
end

Instance Method Details

#handle_action(encoded_message) ⇒ Object



113
114
115
# File 'lib/lite_cable/channel/base.rb', line 113

def handle_action(encoded_message)
  perform_action connection.coder.decode(encoded_message)
end

#handle_subscribeObject



105
106
107
# File 'lib/lite_cable/channel/base.rb', line 105

def handle_subscribe
  subscribed if respond_to?(:subscribed)
end

#handle_unsubscribeObject



109
110
111
# File 'lib/lite_cable/channel/base.rb', line 109

def handle_unsubscribe
  unsubscribed if respond_to?(:unsubscribed)
end