Class: RelayGrid::WebsocketClient

Inherits:
Object
  • Object
show all
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.on_message { |message| puts message["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

Instance Method Summary collapse

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_idObject (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.

Parameters:

  • blocking (Boolean) (defaults to: false)

    block the calling thread until disconnected. Useful for a standalone consumer process; never do this in a web request.



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

Returns:

  • (Boolean)


89
90
91
# File 'lib/relaygrid/websocket_client.rb', line 89

def connected?
  read(:@connected)
end

#disconnectObject



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

Yields:

  • (Exception)


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

Yields:

  • (Hash)

    the message payload, as messages.new_for returns them



45
46
47
48
# File 'lib/relaygrid/websocket_client.rb', line 45

def on_message(&block)
  @message_callbacks << block
  self
end

#subscribed?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/relaygrid/websocket_client.rb', line 93

def subscribed?
  read(:@subscribed)
end