Converse Over a Channel
Learn how to define separate handle_in events for each game.
We'll cover the following...
Bidirectional communications
This is where things start to get interesting. So far, we’ve sent a join message and gotten an ok
back. However, Channels support much richer forms of bidirectional communications between clients and the server. We now take a closer look at the most common bidirectional communications. These will help us while building our own applications.
The simplest way to send a message back from the server is to return a :reply
tuple from the Channel callback. This is a three-element tuple {:reply, some_response, socket}
, where the middle element gets sent back to the client who originally sent the message. This should look really familiar after our work with GenServer
.
Another way to talk back from a Channel is with the push/3
function. push/3
sends the caller back a string that represents a new event along with a data payload. The caller needs ...