Phoenix Channels can build bi-directional communication between message senders and receivers using channels. They are ideal for real-time applications.
Sending and receiving messages is the basic concept of channels. Senders send out messages on a variety of themes. Topics must be subscribed to in order for recipients to receive messages.
Phoenix Channels are a collection of components that work together to provide real-time web capabilities across a distributed system.
A channel’s construction is fairly basic. Channels allow clients to connect to a web server and subscribe to numerous “topics” at a high level. The client then transmits and receives messages over the topics to which it has subscribed. On a single connection, a client may subscribe to as many topics as they like, reducing the number of costly connections. Channels are also transported agnostic, which implies that the same logic may be used by two systems, one using long-polling and the other using WebSockets.
Phoenix Channels are made up of two parts:
The client will first establish a connection to a socket using one of two methods:
Long polling necessitates the creation of JS code that pings the server at regular intervals, looking for changes. WebSockets enable bi-directional communication between the client and the server.
After connecting to the socket, the client utilizes the network connection to join a channel. Then, the
Phoenix.Channel.Server is used to represent this relationship.
Phoenix Channels creates an instance of percent (
Phoenix.Socket) when a client successfully establishes this connection. The channel server keeps track of this socket instance and calls
socket.assign to keep the state within that instance.
A client who connects to a channel through a socket will use the channel to send a message to the channel server process. The message is broadcast out to the local
PubSub server after it is received by the channel server process. This channel server function then transmits the message to any clients on the same server that are connected to the same channel topic. The message is then forwarded by the
PubSub server to any distant PubSub servers operating on other nodes in the
cluster. These PubSub servers then deliver it to their own subscribing clients.
View all Courses