Class: ChatSDK::Slack::SocketMode

Inherits:
Object
  • Object
show all
Defined in:
lib/chat_sdk/slack/socket_mode.rb

Overview

Implements Slack Socket Mode: receives events over a WebSocket instead of incoming HTTP webhooks. Useful for local development, firewalled environments, or any setup that cannot expose a public URL.

Requires the optional faye-websocket gem and an app-level token (xapp-*).

Usage:

adapter.start_socket_mode(app_token: "xapp-...") do |event|
# event is a ChatSDK::Events::* instance
end

Constant Summary collapse

PING_INTERVAL =
30

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_token:, bot_client:) ⇒ SocketMode

Returns a new instance of SocketMode.



20
21
22
23
24
# File 'lib/chat_sdk/slack/socket_mode.rb', line 20

def initialize(app_token:, bot_client:)
  @app_token = app_token
  @bot_client = bot_client
  @running = false
end

Instance Attribute Details

#app_tokenObject (readonly)

Returns the value of attribute app_token.



18
19
20
# File 'lib/chat_sdk/slack/socket_mode.rb', line 18

def app_token
  @app_token
end

#bot_clientObject (readonly)

Returns the value of attribute bot_client.



18
19
20
# File 'lib/chat_sdk/slack/socket_mode.rb', line 18

def bot_client
  @bot_client
end

Instance Method Details

#start(&block) ⇒ Object

Raises:

  • (ArgumentError)


26
27
28
29
30
31
32
33
34
35
36
# File 'lib/chat_sdk/slack/socket_mode.rb', line 26

def start(&block)
  raise ArgumentError, "start requires a block" unless block

  load_websocket_driver!

  @running = true
  while @running
    url = obtain_websocket_url
    run_connection(url, &block)
  end
end

#stopObject



38
39
40
41
# File 'lib/chat_sdk/slack/socket_mode.rb', line 38

def stop
  @running = false
  @ws&.close
end