ChannelClient.Message (channel_client v0.1.1)

Copy Markdown

A struct representing a single Phoenix channel message.

The wire format is determined by the protocol version serializer:

Payloads may be any value the configured json_library can encode, which includes maps, lists, strings, numbers, booleans and nil.

Summary

Functions

Like decode!/3, but returns {:error, reason} instead of raising.

Decodes a raw transport payload into a %Message{}, raising on failure.

Like encode!/3, but returns {:error, reason} instead of raising.

Encodes a %Message{} into its raw transport representation, raising on failure.

Returns the serializer module for a given Phoenix protocol version.

Types

event()

@type event() :: String.t()

payload()

@type payload() :: term()

ref()

@type ref() :: String.t() | nil

t()

@type t() :: %ChannelClient.Message{
  channel_pid: pid() | nil,
  event: event(),
  join_ref: ref(),
  payload: payload(),
  ref: ref(),
  topic: topic()
}

topic()

@type topic() :: String.t()

Functions

decode(serializer, msg, json_library)

@spec decode(module(), term(), module()) :: {:ok, t()} | {:error, term()}

Like decode!/3, but returns {:error, reason} instead of raising.

Used at the socket boundary so that malformed frames from the server are logged and dropped instead of crashing the connection.

decode!(serializer, msg, json_library)

@spec decode!(module(), term(), module()) :: t() | no_return()

Decodes a raw transport payload into a %Message{}, raising on failure.

Transport payloads must be valid JSON produced by the matching protocol version.

encode(serializer, msg, json_library)

@spec encode(module(), t(), module()) :: {:ok, String.t()} | {:error, term()}

Like encode!/3, but returns {:error, reason} instead of raising.

Used so that unencodable payloads (for example tuples or references) are reported to the caller rather than killing the socket process.

encode!(serializer, msg, json_library)

@spec encode!(module(), t(), module()) :: String.t() | no_return()

Encodes a %Message{} into its raw transport representation, raising on failure.

serializer(vsn)

@spec serializer(String.t()) :: module() | no_return()

Returns the serializer module for a given Phoenix protocol version.

Examples

iex> ChannelClient.Message.serializer("2.0.0")
ChannelClient.Message.V2