Close a KubeMQ Elixir Client
Manage the KubeMQ client connection lifecycle and close it cleanly using the Elixir SDK.
Overview
Closing a client isn't an afterthought — it tells the broker and your own process that this connection is done, so both sides release what they were holding for it. A KubeMQ client in Elixir is a GenServer process wrapping a gRPC channel plus whatever subscriptions it's servicing. Skip the close and those linger — the channel stays open and the process stays alive — and in short-lived scripts or test suites you leak both connections and processes until the VM shuts down.
Calling KubeMQ.Client.close/1 stops the GenServer, which releases the gRPC channel and cleans up subscriptions as part of its termination. Once it returns, Process.alive?/1 on that client reference returns false, confirming the process actually terminated.
Gotchas: the GenServer drains what's in flight during termination, not indefinitely, so a slow consumer can still lose the tail of a burst if you close mid-stream; a closed client's process is gone forever — no reconnect on the same reference, start a new start_link; and if you hold the client reference in supervisor state, forgetting to call close/1 before it's discarded leaves an orphaned process running until its supervisor or the VM tears it down.
Prerequisites
- KubeMQ server running on
localhost:50000 - Elixir SDK installed (
{:kubemq, "~> 1.0"}in mix.exs)
Code
IO.puts("Connecting to KubeMQ server...")
{:ok, client} = KubeMQ.Client.start_link(address: "localhost:50000", client_id: "elixir-close-example")
IO.puts("Connected. State: #{KubeMQ.Client.connection_state(client)}")
IO.puts("Closing connection...")
KubeMQ.Client.close(client)
IO.puts("Connection closed. Process alive? #{Process.alive?(client)}")How It Works
close/1stops the GenServer, which releases the gRPC channel and cleans up subscriptions- After closing,
Process.alive?/1returnsfalseconfirming the process has terminated - Always close clients when done to prevent resource leaks
Related
Was this page helpful?