Class: RelayGrid::WebsocketClient
- Inherits:
-
Object
- Object
- RelayGrid::WebsocketClient
- Defined in:
- lib/relaygrid/websocket_client.rb
Overview
Realtime delivery of a recipient's messages over Action Cable.
ws = RelayGrid.websocket(user_id: "user-123")
ws. { || puts ["rendered_subject"] }
ws.connect
Authentication is the same short-lived channel token the send response returns -- it authorises exactly one recipient's stream. Tokens last an hour, so a fresh one is minted on every (re)connect rather than being held for the life of the object.
Constant Summary collapse
- CHANNEL =
"MessagesChannel"- CONTROL_TYPES =
Action Cable's own frame types, which are protocol noise rather than messages a subscriber cares about.
%w[welcome ping confirm_subscription reject_subscription disconnect].freeze
Instance Attribute Summary collapse
-
#user_id ⇒ Object
readonly
Returns the value of attribute user_id.
Instance Method Summary collapse
-
#connect(blocking: false) ⇒ Object
Open the connection and subscribe.
- #connected? ⇒ Boolean
- #disconnect ⇒ Object
-
#initialize(user_id:, client: nil, configuration: nil, reconnect: true) ⇒ WebsocketClient
constructor
A new instance of WebsocketClient.
- #on_close(&block) ⇒ Object
- #on_connect(&block) ⇒ Object
- #on_error {|Exception| ... } ⇒ Object
- #on_message {|Hash| ... } ⇒ Object
- #subscribed? ⇒ Boolean
Constructor Details
#initialize(user_id:, client: nil, configuration: nil, reconnect: true) ⇒ WebsocketClient
Returns a new instance of WebsocketClient.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/relaygrid/websocket_client.rb', line 27 def initialize(user_id:, client: nil, configuration: nil, reconnect: true) @user_id = user_id.to_s @client = client || RelayGrid.client @configuration = configuration || @client.configuration @reconnect = reconnect @message_callbacks = [] @error_callbacks = [] @connect_callbacks = [] @close_callbacks = [] @mutex = Mutex.new @connected = false @subscribed = false @closing = false end |
Instance Attribute Details
#user_id ⇒ Object (readonly)
Returns the value of attribute user_id.
25 26 27 |
# File 'lib/relaygrid/websocket_client.rb', line 25 def user_id @user_id end |
Instance Method Details
#connect(blocking: false) ⇒ Object
Open the connection and subscribe.
71 72 73 74 75 76 77 78 |
# File 'lib/relaygrid/websocket_client.rb', line 71 def connect(blocking: false) @closing = false open_socket wait_until_closed if blocking self end |
#connected? ⇒ Boolean
89 90 91 |
# File 'lib/relaygrid/websocket_client.rb', line 89 def connected? read(:@connected) end |
#disconnect ⇒ Object
80 81 82 83 84 85 86 87 |
# File 'lib/relaygrid/websocket_client.rb', line 80 def disconnect @closing = true set(:@subscribed, false) set(:@connected, false) @socket&.close @socket = nil self end |
#on_close(&block) ⇒ Object
61 62 63 64 |
# File 'lib/relaygrid/websocket_client.rb', line 61 def on_close(&block) @close_callbacks << block self end |
#on_connect(&block) ⇒ Object
56 57 58 59 |
# File 'lib/relaygrid/websocket_client.rb', line 56 def on_connect(&block) @connect_callbacks << block self end |
#on_error {|Exception| ... } ⇒ Object
51 52 53 54 |
# File 'lib/relaygrid/websocket_client.rb', line 51 def on_error(&block) @error_callbacks << block self end |
#on_message {|Hash| ... } ⇒ Object
45 46 47 48 |
# File 'lib/relaygrid/websocket_client.rb', line 45 def (&block) @message_callbacks << block self end |
#subscribed? ⇒ Boolean
93 94 95 |
# File 'lib/relaygrid/websocket_client.rb', line 93 def subscribed? read(:@subscribed) end |