Phoenix.SocketClient.Message (phoenix_socket_client v0.8.1)
View SourceDefines the message structure and protocol for Phoenix Channels communication.
This module provides message encoding/decoding for Phoenix Channels V2 protocol 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 Phoenix Channels V2 protocol:
- V2: Current protocol with improved message structure and performance
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 (V2)msg- Raw JSON message stringjson_library- JSON library module (e.g.,JSONorJason)
Returns
Phoenix.SocketClient.Message.t()- Decoded message struct
Examples
message = Phoenix.SocketClient.Message.decode!(V2, raw_json, JSON)
Encodes a Message struct into a JSON string.
Parameters
serializer- Serializer module (V2)msg- Message struct to encodejson_library- JSON library module (e.g.,JSONorJason)
Returns
- String - JSON-encoded message
Examples
json = Phoenix.SocketClient.Message.encode!(V2, message, JSON)
@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 (supports V2 and higher)
Returns
- Module - Serializer module for the protocol version
Deprecation Notice
V1 protocol has been removed for performance and maintainability. Use V2 protocol ("2.0.0") for new applications.