EDA.Gateway.Connection (EDA v0.3.0)

Copy Markdown View Source

WebSocket connection to Discord Gateway.

Each instance represents a single shard, registered via EDA.Gateway.Registry for pid-accurate routing.

Handles:

  • WebSocket connection lifecycle
  • Heartbeat management
  • Event reception and dispatch
  • Resume/reconnect logic

Summary

Functions

Returns connection metadata for a shard: latency, uptime, heartbeat interval.

Returns the heartbeat latency in milliseconds for a shard, or nil if unknown.

Sends a message to a channel. Convenience function.

Starts the Gateway connection for a shard.

Sends OP 4 (Voice State Update) to join/leave a voice channel.

Types

t()

@type t() :: %EDA.Gateway.Connection{
  connected_at: integer() | nil,
  encoding: module() | nil,
  heartbeat_ack: boolean(),
  heartbeat_interval: integer() | nil,
  heartbeat_latency: integer() | nil,
  heartbeat_ref: reference() | nil,
  heartbeat_send_time: integer() | nil,
  hello_timer: reference() | nil,
  resume_gateway_url: String.t() | nil,
  seq: integer() | nil,
  session_id: String.t() | nil,
  shard: {integer(), integer()},
  token: String.t(),
  zlib: EDA.Gateway.Zlib.t() | nil
}

Functions

connection_info(shard_id)

@spec connection_info(non_neg_integer()) :: map() | nil

Returns connection metadata for a shard: latency, uptime, heartbeat interval.

heartbeat_latency(shard_id)

@spec heartbeat_latency(non_neg_integer()) :: non_neg_integer() | nil

Returns the heartbeat latency in milliseconds for a shard, or nil if unknown.

send_message(channel_id, content)

Sends a message to a channel. Convenience function.

start_link(opts)

Starts the Gateway connection for a shard.

Options

  • :token — bot token (required)
  • :shard — {shard_id, total_shards} tuple (required)
  • :gateway_url — WebSocket URL from /gateway/bot (required)

update_voice_state(guild_id, channel_id, opts)

Sends OP 4 (Voice State Update) to join/leave a voice channel.

Routes to the correct shard based on guild_id. Set channel_id to nil to leave the voice channel.