Class: BoltRb::SocketMode::Client
- Inherits:
-
Object
- Object
- BoltRb::SocketMode::Client
- Defined in:
- lib/bolt_rb/socket_mode/client.rb
Overview
WebSocket client for Slack Socket Mode connections
Handles the WebSocket lifecycle including:
- Obtaining a connection URL via apps.connections.open
- Establishing and maintaining the WebSocket connection
- Acknowledging received events
- Automatic reconnection on disconnect
Constant Summary collapse
- SLACK_API_URL =
'https://slack.com/api/apps.connections.open'- RECONNECT_DELAY =
5- CONNECTION_STALE_THRESHOLD =
If no messages received in this many seconds, assume zombie socket
45
Instance Attribute Summary collapse
-
#app_token ⇒ String
readonly
The Slack app-level token.
-
#logger ⇒ Logger
readonly
The logger instance.
Instance Method Summary collapse
-
#connected? ⇒ Boolean
Whether the WebSocket is connected.
-
#healthy?(threshold) ⇒ Boolean
Checks if the connection is healthy based on recent message activity.
-
#initialize(app_token:, logger: nil) ⇒ Client
constructor
Creates a new Socket Mode client.
-
#on_message {|Hash| ... } ⇒ void
Registers a handler for incoming messages.
-
#request_stop ⇒ void
Requests a stop - safe to call from trap context.
-
#running? ⇒ Boolean
Whether the client is currently running.
-
#start ⇒ void
Starts the Socket Mode connection.
-
#stop ⇒ void
Stops the Socket Mode connection.
Constructor Details
#initialize(app_token:, logger: nil) ⇒ Client
Creates a new Socket Mode client
41 42 43 44 45 46 47 48 |
# File 'lib/bolt_rb/socket_mode/client.rb', line 41 def initialize(app_token:, logger: nil) @app_token = app_token @logger = logger || Logger.new($stdout) @running = false @websocket = nil = [] = nil end |
Instance Attribute Details
#app_token ⇒ String (readonly)
Returns The Slack app-level token.
32 33 34 |
# File 'lib/bolt_rb/socket_mode/client.rb', line 32 def app_token @app_token end |
#logger ⇒ Logger (readonly)
Returns The logger instance.
35 36 37 |
# File 'lib/bolt_rb/socket_mode/client.rb', line 35 def logger @logger end |
Instance Method Details
#connected? ⇒ Boolean
Returns Whether the WebSocket is connected.
95 96 97 |
# File 'lib/bolt_rb/socket_mode/client.rb', line 95 def connected? @websocket&.open? end |
#healthy?(threshold) ⇒ Boolean
Checks if the connection is healthy based on recent message activity
103 104 105 106 107 |
# File 'lib/bolt_rb/socket_mode/client.rb', line 103 def healthy?(threshold) return false if .nil? Time.now - <= threshold end |
#on_message {|Hash| ... } ⇒ void
This method returns an undefined value.
Registers a handler for incoming messages
54 55 56 |
# File 'lib/bolt_rb/socket_mode/client.rb', line 54 def (&block) << block end |
#request_stop ⇒ void
This method returns an undefined value.
Requests a stop - safe to call from trap context
Only sets the running flag to false. Does NOT close the websocket or perform any operations that might use mutexes, as this is designed to be called from signal trap handlers.
85 86 87 |
# File 'lib/bolt_rb/socket_mode/client.rb', line 85 def request_stop @running = false end |
#running? ⇒ Boolean
Returns Whether the client is currently running.
90 91 92 |
# File 'lib/bolt_rb/socket_mode/client.rb', line 90 def running? @running end |
#start ⇒ void
This method returns an undefined value.
Starts the Socket Mode connection
Obtains a WebSocket URL and establishes the connection. This method blocks until stop is called.
64 65 66 67 68 |
# File 'lib/bolt_rb/socket_mode/client.rb', line 64 def start @running = true connect_with_retry run_loop end |
#stop ⇒ void
This method returns an undefined value.
Stops the Socket Mode connection
73 74 75 76 |
# File 'lib/bolt_rb/socket_mode/client.rb', line 73 def stop @running = false @websocket&.close end |