Module: Rage::Cable

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

Defined Under Namespace

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

Class Method Summary collapse

Class Method Details

.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
  protocol = Rage.config.cable.protocol
  protocol.init(__router)

  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) ⇒ Object

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.



99
100
101
# File 'lib/rage/cable/cable.rb', line 99

def self.broadcast(stream, data)
  Rage.config.cable.protocol.broadcast(stream, data)
end