Question

I have two nodes that are connected to each other, where one of them is the server. The server would like to know if the client dies. I did something like this:

link(Client).

In the server process, and when I did that I receive a exception error: noconnection and then the server dies, when the client dies. I would just like to know if the client dies, I do not want the server do die, how do I handle the death message?

Was it helpful?

Solution

If you have two erlang nodes and want to take some actions in case if one node goes down (or network connection is lost) you possible want to use erlang:monitor_node/2,3 functions:

(n1@myhost)1> erlang:monitor_node('n2@myhost', true).
true

then if 'n2@myhost' node goes down your process will receive message:

(n1@myhost)2> flush().
Shell got {nodedown,n2@myhost}

(note, I did that from erlang shell, that is why I may call flush/0 to see what is in the mailbox of the shell process)

If you interested in certain process, on the second node you may use erlang:monitor/2

(n1@myhost)3> Ref = erlang:monitor(process, {'n2@myhost', some_registered_name}).
#Ref<0.0.0.117>

from now you will receive a message if some_registered_name goes down and you can take an action.

Also you may be interested in how to write distributed applications

OTHER TIPS

To have unidirectional supervision, you should use monitors. Then your server will receive a message if the client dies.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top