Class: Whatsapp::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/whatsapp/connection.rb

Overview

Manages the Node.js bridge process lifecycle.

Spawns the bridge server on a random available port, waits for it to become ready, and provides URLs for HTTP and WebSocket communication.

Examples:

conn = Whatsapp::Connection.new(config)
conn.start!
conn.base_url  # => "http://127.0.0.1:54321"
conn.ws_url    # => "ws://127.0.0.1:54321/ws"
conn.stop!

Constant Summary collapse

BRIDGE_DIR =

Path to the bridge directory containing the Node.js server.

File.expand_path("../../bridge", __dir__)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = Whatsapp.configuration) ⇒ Connection

Returns a new instance of Connection.

Parameters:

  • config (Configuration) (defaults to: Whatsapp.configuration)

    gem configuration



31
32
33
34
35
# File 'lib/whatsapp/connection.rb', line 31

def initialize(config = Whatsapp.configuration)
  @config = config
  @pid = nil
  @port = nil
end

Instance Attribute Details

#pidInteger? (readonly)

Returns PID of the bridge process.

Returns:

  • (Integer, nil)

    PID of the bridge process



28
29
30
# File 'lib/whatsapp/connection.rb', line 28

def pid
  @pid
end

#portInteger? (readonly)

Returns port the bridge is listening on.

Returns:

  • (Integer, nil)

    port the bridge is listening on



25
26
27
# File 'lib/whatsapp/connection.rb', line 25

def port
  @port
end

Instance Method Details

#alive?Boolean

Check if the bridge process is still running.

Returns:

  • (Boolean)


90
91
92
93
94
95
96
97
# File 'lib/whatsapp/connection.rb', line 90

def alive?
  return false unless @pid

  Process.kill(0, @pid)
  true
rescue Errno::ESRCH
  false
end

#base_urlString

HTTP base URL for the bridge.

Returns:

Raises:



103
104
105
106
107
# File 'lib/whatsapp/connection.rb', line 103

def base_url
  raise BridgeNotRunning unless @port

  "http://127.0.0.1:#{@port}"
end

#start!void

This method returns an undefined value.

Start the Node.js bridge process.

Installs npm dependencies if needed, finds an available port, spawns the process, and waits for it to become ready.

Raises:



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/whatsapp/connection.rb', line 45

def start!
  verify_node!
  install_deps! unless deps_installed?

  ensure_secure_auth_dir!
  disable_core_dumps!

  @port = find_available_port

  @pid = Process.spawn(
    {
      "BRIDGE_PORT" => @port.to_s,
      "AUTH_DIR" => @config.auth_dir
    },
    @config.node_command, "server.mjs",
    chdir: BRIDGE_DIR,
    out: log_io,
    err: log_io
  )

  Process.detach(@pid)
  wait_for_ready!
end

#stop!void

This method returns an undefined value.

Stop the bridge process.

Sends SIGTERM, waits briefly, then SIGKILL if still alive.



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/whatsapp/connection.rb', line 74

def stop!
  return unless @pid

  Process.kill("TERM", @pid)
  sleep 0.5
  Process.kill("KILL", @pid) if alive?
rescue Errno::ESRCH
  # already dead
ensure
  @pid = nil
  @port = nil
end

#ws_urlString

WebSocket URL for real-time events.

Returns:

  • (String)

    e.g. "ws://127.0.0.1:54321/ws"

Raises:



113
114
115
116
117
# File 'lib/whatsapp/connection.rb', line 113

def ws_url
  raise BridgeNotRunning unless @port

  "ws://127.0.0.1:#{@port}/ws"
end