Module: Rage::Cable
- Defined in:
- lib/rage/cable/cable.rb
Overview
Rage::Cable provides built-in WebSocket support for Rage apps, similar to Action Cable in Rails. It lets you mount a separate WebSocket application, define channels and connections, subscribe clients to named streams, and broadcast messages in real time.
Define a channel:
class ChatChannel < Rage::Cable::Channel
def subscribed
stream_from "chat"
end
def receive(data)
puts "Received message: #{data['message']}"
end
end
Mount the Cable application:
Rage.routes.draw do
mount Rage::Cable.application, at: "/cable"
end
Broadcast a message to a stream:
Rage.cable.broadcast("chat", { message: "Hello, world!" })
Defined Under Namespace
Modules: Adapters, Protocols Classes: Channel, Connection, Router, WebSocketConnection
Constant Summary collapse
- Protocol =
Protocols
Class Method Summary collapse
-
.application ⇒ Object
Create a new Cable application.
-
.broadcast(stream, data) ⇒ true
Broadcast data directly to a named stream.
Class Method Details
.application ⇒ Object
Create a new Cable application.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/rage/cable/cable.rb', line 38 def self.application # explicitly initialize the adapter __adapter handler = __build_handler(__protocol) accept_response = [0, __protocol.protocol_definition, []] application = ->(env) do Rage::Telemetry.tracer.span_cable_websocket_handshake(env:) do if env["rack.upgrade?"] == :websocket env["rack.upgrade"] = handler accept_response else [426, { "connection" => "upgrade", "upgrade" => "websocket" }, []] end end end chain = Rage.with_middlewares(application, Rage.config.cable.middlewares) application.define_singleton_method(:__rage_app_name) { "Rage::Cable" } chain.define_singleton_method(:__rage_root_app) { application } chain end |
.broadcast(stream, data) ⇒ true
Broadcast data directly to a named stream.
141 142 143 144 145 146 147 148 |
# File 'lib/rage/cable/cable.rb', line 141 def self.broadcast(stream, data) Rage::Telemetry.tracer.span_cable_stream_broadcast(stream:) do __protocol.broadcast(stream, data) __adapter&.publish(stream, data) end true end |