Class: Tina4::WebSocketBackplane

Inherits:
Object
  • Object
show all
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

NATSBackplane, RedisBackplane

Class Method Summary collapse

Instance Method Summary collapse

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

#closeObject

Tear down connections and background threads.

Raises:

  • (NotImplementedError)


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.

Raises:

  • (NotImplementedError)


27
28
29
# File 'lib/tina4/websocket_backplane.rb', line 27

def publish(channel, message)
  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.

Raises:

  • (NotImplementedError)


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.

Raises:

  • (NotImplementedError)


38
39
40
# File 'lib/tina4/websocket_backplane.rb', line 38

def unsubscribe(channel)
  raise NotImplementedError, "#{self.class}#unsubscribe not implemented"
end