Class: BoltRb::SocketMode::Client

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

Examples:

Basic usage

client = BoltRb::SocketMode::Client.new(
  app_token: 'xapp-...',
  logger: Logger.new(STDOUT)
)
client.on_message { |payload| handle_event(payload) }
client.start

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

Instance Method Summary collapse

Constructor Details

#initialize(app_token:, logger: nil) ⇒ Client

Creates a new Socket Mode client

Parameters:

  • app_token (String)

    The Slack app-level token (xapp-...)

  • logger (Logger) (defaults to: nil)

    Logger instance for output



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
  @message_handlers = []
  @last_message_at = nil
end

Instance Attribute Details

#app_tokenString (readonly)

Returns The Slack app-level token.

Returns:

  • (String)

    The Slack app-level token



32
33
34
# File 'lib/bolt_rb/socket_mode/client.rb', line 32

def app_token
  @app_token
end

#loggerLogger (readonly)

Returns The logger instance.

Returns:

  • (Logger)

    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.

Returns:

  • (Boolean)

    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

Parameters:

  • threshold (Numeric)

    Maximum seconds since last message to be considered healthy

Returns:

  • (Boolean)

    True if last message was received within threshold, false otherwise



103
104
105
106
107
# File 'lib/bolt_rb/socket_mode/client.rb', line 103

def healthy?(threshold)
  return false if @last_message_at.nil?

  Time.now - @last_message_at <= threshold
end

#on_message {|Hash| ... } ⇒ void

This method returns an undefined value.

Registers a handler for incoming messages

Yields:

  • (Hash)

    The parsed event payload



54
55
56
# File 'lib/bolt_rb/socket_mode/client.rb', line 54

def on_message(&block)
  @message_handlers << block
end

#request_stopvoid

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.

Returns:

  • (Boolean)

    Whether the client is currently running



90
91
92
# File 'lib/bolt_rb/socket_mode/client.rb', line 90

def running?
  @running
end

#startvoid

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

#stopvoid

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