Implementation of a Channel
Explore how to implement a Phoenix Channel by creating a PingChannel that handles real-time client connections and messages. Understand channel responsibilities, the distinction from sockets, message handling, and how to test the channel using WebSocket tools.
Channels
Channels are the real-time entry points to our application’s logic and where most applications’ request handling code lives. A Channel has several different responsibilities to enable real-time applications:
- Accept or reject a request to join.
- Handle messages from the client.
- Handle messages from the PubSub.
- Push messages to the client.
Channels,Controllers, and Sockets
The distinction between Channels and Sockets may not be evident at a glance. A Socket’s responsibilities involve connection handling and routing requests to the correct Channel. A Channel’s responsibilities include handling requests from a client and sending data to a client. In this way, a Channel is similar to a Controller in the ...