EDA.Gateway.Encoding behaviour (EDA v0.3.0)

Copy Markdown View Source

Behaviour for encoding/decoding Discord Gateway payloads.

Two implementations are provided:

Configuration

config :eda, gateway_encoding: :etf   # default
config :eda, gateway_encoding: :json

Interaction with zlib

Both encodings work with zlib-stream compression. The gateway connection decompresses frames before passing them to decode/1, so the encoding module always receives raw (uncompressed) binary data.

Summary

Callbacks

Decodes a raw gateway payload binary into a map with string keys.

Encodes a map into a gateway payload ready to send over WebSocket.

Returns the encoding name for the gateway URL query parameter ("json" or "etf").

Functions

Returns the configured encoding module.

Callbacks

decode(binary)

@callback decode(binary()) :: map()

Decodes a raw gateway payload binary into a map with string keys.

encode(map)

@callback encode(map()) :: {:text | :binary, binary()}

Encodes a map into a gateway payload ready to send over WebSocket.

Returns {:text, binary()} for JSON or {:binary, binary()} for ETF, matching the frame types expected by WebSockex.

url_encoding()

@callback url_encoding() :: String.t()

Returns the encoding name for the gateway URL query parameter ("json" or "etf").

Functions

module()

@spec module() :: module()

Returns the configured encoding module.

Reads :gateway_encoding from the :eda application config. Defaults to EDA.Gateway.Encoding.ETF.