ExESDBGater.Messages (ex_esdb_gater v0.8.0)

Main module providing access to all ExESDBGater message definitions and helpers.

This module serves as a central entry point for all PubSub message types used throughout the ExESDBGater system.

Message Modules

Each PubSub instance has its own dedicated message module:

  • SystemMessages - General system configuration and lifecycle events (:ex_esdb_system)
  • HealthMessages - Health monitoring and status checks (:ex_esdb_health)
  • MetricsMessages - Performance metrics and measurements (:ex_esdb_metrics)
  • LifecycleMessages - Process and node lifecycle events (:ex_esdb_lifecycle)
  • SecurityMessages - Security events and access control (:ex_esdb_security)
  • AuditMessages - Audit trail and compliance events (:ex_esdb_audit)
  • AlertMessages - Critical alerts and notifications (:ex_esdb_alerts)
  • DiagnosticsMessages - Deep diagnostic and debugging info (:ex_esdb_diagnostics)
  • LoggingMessages - Log aggregation and distribution (:ex_esdb_logging)

Consistent Node Tracking Pattern

All message structs now include consistent node tracking fields to support distributed troubleshooting and monitoring:

  • :node - Standard field for most messages, tracks the originating node
  • :originating_node - Used in cluster membership events to distinguish from affected nodes in the event
  • :reporting_node - Used in metrics/monitoring where the reporter may differ from the event source

All message creation functions use MessageHelpers.get_node(opts) which defaults to Node.self() but can be overridden via options.

Standardized Timestamps

All messages use MessageHelpers.current_timestamp() which provides millisecond-precision UTC timestamps for consistency across the cluster.

Usage Examples

# Using system messages
alias ExESDBGater.Messages.SystemMessages

# Create and broadcast a config change
payload = SystemMessages.system_config(:database, %{pool_size: 10})
SystemMessages.broadcast_system_config("config", payload)

# Subscribe to health updates
Phoenix.PubSub.subscribe(:ex_esdb_health, "node_health")

# Validate received messages - using pattern matching
def handle_info({:secure_message, _sig, {:node_health_updated, _payload}} = message, state) do
  case ExESDBGater.Messages.HealthMessages.validate_secure_message(message) do
    {:ok, {:node_health_updated, payload}} ->
      handle_health_update(payload, state)
    {:error, _reason} ->
      {:noreply, state}
  end
end

def handle_info(_message, state), do: {:noreply, state}

Security

All message modules include HMAC-based security using SECRET_KEY_BASE to prevent unauthorized messages from entering the system. Messages are automatically signed when broadcast and can be validated by receivers.

Summary

Functions

Returns a list of all message modules.

Returns a map of PubSub instance atoms to their corresponding message modules.

Returns the message module for a given PubSub instance.

Returns a list of all supported PubSub instances.

Validates a message using the appropriate module for the given PubSub instance.

Functions

all_modules()

Returns a list of all message modules.

instance_to_module_map()

Returns a map of PubSub instance atoms to their corresponding message modules.

Useful for dynamic message handling or routing.

module_for_instance(instance)

Returns the message module for a given PubSub instance.

Examples

iex> ExESDBGater.Messages.module_for_instance(:ex_esdb_health)
ExESDBGater.Messages.HealthMessages

iex> ExESDBGater.Messages.module_for_instance(:invalid)
nil

supported_instances()

Returns a list of all supported PubSub instances.

validate_message(instance, message)

Validates a message using the appropriate module for the given PubSub instance.

Examples

iex> ExESDBGater.Messages.validate_message(:ex_esdb_health, message)
{:ok, {:node_health_updated, payload}}