Phoenix.SocketClient.Message (phoenix_socket_client v0.7.0)
Defines the message structure and protocol for Phoenix Channels communication.
This module provides message encoding/decoding and handles both V1 and V2 Phoenix Channels protocol versions. It defines the structure for messages sent between client and server.
Message Structure
The message struct contains:
:topic- The channel topic (e.g., "rooms:lobby"):event- The event name (e.g., "phx_join", "new_msg"):payload- The message payload (any term):channel_pid- The channel process PID (internal use):ref- Message reference for reply matching:join_ref- Reference for channel joins
Protocol Versions
Supports both V1 and V2 Phoenix Channels protocols:
- V1: Legacy protocol with different message format
- V2: Current protocol with improved message structure
Summary
Functions
Decodes a raw message string into a Message struct.
Encodes a Message struct into a JSON string.
Generates a unique reference string for message correlation.
Creates a join message for channel subscription.
Creates a leave message for channel unsubscription.
Creates a push message for sending data to a channel.
Returns the appropriate serializer module for the given protocol version.
Types
Functions
Decodes a raw message string into a Message struct.
Parameters
serializer- Serializer module (V1 or V2)msg- Raw JSON message stringjson_library- JSON library module (e.g., Jason)
Returns
Phoenix.SocketClient.Message.t()- Decoded message struct
Examples
message = Phoenix.SocketClient.Message.decode!(V2, raw_json, Jason)
Encodes a Message struct into a JSON string.
Parameters
serializer- Serializer module (V1 or V2)msg- Message struct to encodejson_library- JSON library module (e.g., Jason)
Returns
- String - JSON-encoded message
Examples
json = Phoenix.SocketClient.Message.encode!(V2, message, Jason)
@spec generate_ref() :: String.t()
Generates a unique reference string for message correlation.
Returns
- String - Unique reference string
Creates a join message for channel subscription.
Parameters
topic- Channel topic (e.g., "rooms:lobby")params- Join parameters (map or keyword list)
Returns
Phoenix.SocketClient.Message.t()- Join message
Examples
message = Phoenix.SocketClient.Message.join("rooms:lobby", %{user_id: 123})
Creates a leave message for channel unsubscription.
Parameters
topic- Channel topic to leave
Returns
Phoenix.SocketClient.Message.t()- Leave message
Examples
message = Phoenix.SocketClient.Message.leave("rooms:lobby")
Creates a push message for sending data to a channel.
Parameters
topic- Channel topicevent- Event name (e.g., "new_msg")payload- Message payload
Returns
Phoenix.SocketClient.Message.t()- Push message
Examples
message = Phoenix.SocketClient.Message.push("rooms:lobby", "new_msg", %{body: "Hello"})
Returns the appropriate serializer module for the given protocol version.
Parameters
vsn- Protocol version string ("1.0.0" or "2.0.0")
Returns
- Module - Serializer module for the protocol version
Deprecation Notice
V1 protocol ("1.0.0") is deprecated and will be removed in a future version. Use V2 protocol ("2.0.0") for new applications.