Module: Rage::Cable

Defined in:
lib/rage/cable/cable.rb

Defined Under Namespace

Modules: Adapters, Protocol Classes: Channel, Connection, Router, WebSocketConnection

Class Method Summary collapse

Class Method Details

.__adapterObject



39
40
41
# File 'lib/rage/cable/cable.rb', line 39

def self.__adapter
  @__adapter ||= Rage.config.cable.adapter
end

.applicationObject

Create a new Cable application.

Examples:

map "/cable" do
  run Rage.cable.application
end


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rage/cable/cable.rb', line 10

def self.application
  # explicitly initialize the adapter
  __adapter

  handler = __build_handler(__protocol)
  accept_response = [0, __protocol.protocol_definition, []]

  application = ->(env) do
    if env["rack.upgrade?"] == :websocket
      env["rack.upgrade"] = handler
      accept_response
    else
      [426, { "Connection" => "Upgrade", "Upgrade" => "websocket" }, []]
    end
  end

  Rage.with_middlewares(application, Rage.config.cable.middlewares)
end

.broadcast(stream, data) ⇒ true

Broadcast data directly to a named stream.

Examples:

Rage.cable.broadcast("chat", { message: "A new member has joined!" })

Parameters:

  • stream (String)

    the name of the stream

  • data (Object)

    the object to send to the clients. This will later be encoded according to the protocol used.

Returns:

  • (true)


107
108
109
110
111
112
# File 'lib/rage/cable/cable.rb', line 107

def self.broadcast(stream, data)
  __protocol.broadcast(stream, data)
  __adapter&.publish(stream, data)

  true
end