Class: Whatsapp::Connection
- Inherits:
-
Object
- Object
- Whatsapp::Connection
- 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.
Constant Summary collapse
- BRIDGE_DIR =
Path to the bridge directory containing the Node.js server.
File.("../../bridge", __dir__)
Instance Attribute Summary collapse
-
#pid ⇒ Integer?
readonly
PID of the bridge process.
-
#port ⇒ Integer?
readonly
Port the bridge is listening on.
Instance Method Summary collapse
-
#alive? ⇒ Boolean
Check if the bridge process is still running.
-
#base_url ⇒ String
HTTP base URL for the bridge.
-
#initialize(config = Whatsapp.configuration) ⇒ Connection
constructor
A new instance of Connection.
-
#start! ⇒ void
Start the Node.js bridge process.
-
#stop! ⇒ void
Stop the bridge process.
-
#ws_url ⇒ String
WebSocket URL for real-time events.
Constructor Details
#initialize(config = Whatsapp.configuration) ⇒ Connection
Returns a new instance of Connection.
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
#pid ⇒ Integer? (readonly)
Returns PID of the bridge process.
28 29 30 |
# File 'lib/whatsapp/connection.rb', line 28 def pid @pid end |
#port ⇒ Integer? (readonly)
Returns 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.
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_url ⇒ String
HTTP base URL for the bridge.
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.
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_url ⇒ String
WebSocket URL for real-time events.
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 |