Class: Tina4::WebSocketBackplane
- Inherits:
-
Object
- Object
- Tina4::WebSocketBackplane
- Defined in:
- lib/tina4/websocket_backplane.rb
Overview
Base backplane interface for scaling WebSocket broadcast across instances.
Subclasses implement publish/subscribe over a shared message bus so that every server instance receives every broadcast, not just the originator.
Direct Known Subclasses
Class Method Summary collapse
-
.create_backplane(url: nil) ⇒ Object
Factory that reads TINA4_WS_BACKPLANE and returns the appropriate backplane instance, or
nilif no backplane is configured.
Instance Method Summary collapse
-
#close ⇒ Object
Tear down connections and background threads.
-
#publish(channel, message) ⇒ Object
Publish a message to all instances listening on
channel. -
#subscribe(channel, &block) ⇒ Object
Subscribe to
channel. -
#unsubscribe(channel) ⇒ Object
Stop listening on
channel.
Class Method Details
.create_backplane(url: nil) ⇒ Object
Factory that reads TINA4_WS_BACKPLANE and returns the appropriate backplane instance, or nil if no backplane is configured.
This keeps backplane usage entirely optional — callers simply check if backplane before publishing.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/tina4/websocket_backplane.rb', line 52 def self.create_backplane(url: nil) backend = ENV.fetch("TINA4_WS_BACKPLANE", "").strip.downcase case backend when "redis" RedisBackplane.new(url: url) when "nats" NATSBackplane.new(url: url) when "" nil else raise ArgumentError, "Unknown TINA4_WS_BACKPLANE value: '#{backend}'" end end |
Instance Method Details
#close ⇒ Object
Tear down connections and background threads.
43 44 45 |
# File 'lib/tina4/websocket_backplane.rb', line 43 def close raise NotImplementedError, "#{self.class}#close not implemented" end |
#publish(channel, message) ⇒ Object
Publish a message to all instances listening on channel.
27 28 29 |
# File 'lib/tina4/websocket_backplane.rb', line 27 def publish(channel, ) raise NotImplementedError, "#{self.class}#publish not implemented" end |
#subscribe(channel, &block) ⇒ Object
Subscribe to channel. The block is called with each incoming message. Runs in a background thread.
33 34 35 |
# File 'lib/tina4/websocket_backplane.rb', line 33 def subscribe(channel, &block) raise NotImplementedError, "#{self.class}#subscribe not implemented" end |
#unsubscribe(channel) ⇒ Object
Stop listening on channel.
38 39 40 |
# File 'lib/tina4/websocket_backplane.rb', line 38 def unsubscribe(channel) raise NotImplementedError, "#{self.class}#unsubscribe not implemented" end |