Coder
Coder

Reputation: 428

Message Evaluation

When my GenServer is receiving messages from outside queue/topic it always ends up n handle_info [being called from outside].So, just wanted to know,

  1. Is is good to have all messages received this way as i have to call function by evaluating the messages all the time, or is there any other way as well. For example, User sharing his details, sending message to other users etc.

    I created one GenServer and started the process and now clients can send messages to some queue to register their details[name, phone no etc].

  2. Now, when new message received from client i can not get any PID from handle_info. So, how can i make process specific to that user.

    def handle_info({_, data}, state) do
      {:noreply, state}
    end

Upvotes: 0

Views: 88

Answers (1)

mpm
mpm

Reputation: 3584

Since you are using GenServer (or erlangs gen_server) you should not send messeages to process explicitly (with !) but rather use interface functions like call (if you expect some value returned from server) and cast (if you just want to send some date, and don't need to wait for response.

Second thing is you should also not expose your module clients to GenServer interface. Rather than making them call GenServer:call you should wrap it in some funtion in your module. If you implementing some king you counter you could write something like this

def increase(counter) do 
   GenServer.cast(conter, {:increase})
done

def get_count(counter) do 
   GenServer.call(counter, {get_coung})
done

and handle both of them in respective handle... functions.

Upvotes: 1

Related Questions