MqttX.Session.ETSStore (MqttX v0.13.0)

Copy Markdown View Source

ETS-based in-memory session store.

The built-in MqttX.Session.Store implementation. Sessions live in an ETS table owned by a supervised process, so they survive a connection crash and persist for the lifetime of the BEAM VM. Session persistence is off unless you pass :session_store explicitly to MqttX.Client.connect/1.

Options

  • :table - Name of the ETS table (default: :mqttx_sessions)

Usage

{:ok, client} = MqttX.Client.connect(
  host: "localhost",
  client_id: "my_client",
  clean_session: false,
  session_store: MqttX.Session.ETSStore
)

# Or with custom table name
{:ok, client} = MqttX.Client.connect(
  host: "localhost",
  client_id: "my_client",
  clean_session: false,
  session_store: {MqttX.Session.ETSStore, table: :my_sessions}
)

Expiry

Records carry expires_at (see MqttX.Session.Store). The application's table owner (MqttX.Session.ETSOwner) sweeps every table it owns on an interval (default 60s, configurable with config :mqttx, session_sweep_interval: ms), deleting expired records; sweep_expired/2 runs the same sweep on demand.

Limitations

  • Sessions are lost when the BEAM VM restarts
  • Not suitable for distributed deployments (sessions are node-local)

For persistent storage across restarts, implement a custom store using the MqttX.Session.Store behaviour with your preferred database.

Summary

Functions

Delete every session in table whose expires_at is at or before now (unix seconds). Returns the number of sessions deleted.

Functions

sweep_expired(table, now \\ System.system_time(:second))

@spec sweep_expired(atom() | :ets.tid(), integer()) :: non_neg_integer()

Delete every session in table whose expires_at is at or before now (unix seconds). Returns the number of sessions deleted.

Records without an integer expires_at (pre-5 sessions, 0xFFFFFFFF, or records written before expiry was tracked) are never swept.