concept response in category elixir

appears as: response, response, responses, responses
Elixir in Action

This is an excerpt from Manning's book Elixir in Action.

The basic message-passing mechanism is the asynchronous “fire and forget” kind. A process sends a message and then continues to run, oblivious to what happens in the receiver. Sometimes a caller needs some kind of response from the receiver. There’s no special language construct for doing this. Instead, you must program both parties to cooperate using the basic asynchronous messaging facility.

The caller must include its own pid in the message contents and then wait for a response from the receiver. The receiver uses the embedded pid to send the response to the caller, as illustrated in figure 5.3. You’ll see this in action a bit later, when we discuss server processes.

The generic code will perform various tasks common to server processes, leaving the specific decisions to concrete implementations. For example, the generic code will spawn a process, but the concrete implementation must determine the initial state. Similarly, the generic code will receive a message and optionally send the response, but the concrete implementation must decide how the message is handled and what the response is.

The generic code is responsible for receiving and sending messages, whereas the specific implementation must handle the message and return the response and the new state. The idea is illustrated in the following listing.

Listing 6.2 Handling messages in the server process (server_process.ex)
defmodule ServerProcess do
  ...
  defp loop(callback_module, current_state) do
    receive do
      {request, caller} ->
        {response, new_state} =
 callback_module.handle_call(    #1  
 request,    #1  
 current_state    #1  
 )    #1  

 send(caller, {:response, response})    #2  

 loop(callback_module, new_state)    #3  
    end
  end
  ...
end

#1   Invokes the callback to handle the message
#2   Sends the response back
#3   Loops with the new state

Here, you expect a message in the form of a {request, caller} tuple. The request is data that identifies the request and is meaningful to the specific implementation. The callback function handle_call/2 takes the request payload and the current state, and it must return a {response, new_state} tuple. The generic code can then send the response back to the caller and continue looping with the new state.

Elixir in Action

This is an excerpt from Manning's book Elixir in Action.

defp process_message(current_value, {:value, caller}) do
  send(caller, {:response, current_value})
  current_value
end

defp process_message(current_value, {:add, value}) do
  current_value + value
end

...

The loop handles various messages. The value message is used to retrieve the server’s state. Because you need to send the response back, the caller must include its pid in the message. Notice that the last expression of this block returns current_value. This is needed because the result of receive is stored in new_value, which is then used as the server’s new state. By returning current_value, you specify that the value request doesn’t change the process state.

The arithmetic operations compute the new state based on the current value and the argument received in the message. Unlike a value message handler, arithmetic operation handlers don’t send responses back to the caller. This makes it possible to run these operations asynchronously, as you’ll see soon when you implement interface functions.

The generic code will perform various tasks common to server processes, leaving the specific decisions to concrete implementations. For example, the generic code will spawn a process, but the concrete implementation must determine the initial state. Similarly, the generic code will receive a message and optionally send the response, but the concrete implementation must decide how the message is handled and what the response is.

The generic code is responsible for receiving and sending messages, whereas the specific implementation must handle the message and return the response and the new state. The idea is illustrated in the following listing.

Listing 6.2. Handling messages in the server process (server_process.ex)

Here, you expect a message in the form of a {request, caller} tuple. The request is data that identifies the request and is meaningful to the specific implementation. The callback function handle_call/2 takes the request payload and the current state, and it must return a {response, new_state} tuple. The generic code can then send the response back to the caller and continue looping with the new state.