Search code examples
erlangyaws

How can I trap process exits in yaws?


A separate Erlang chat server stores all PIDs in a mnesia table and groups them under a Room ID so when a user sends a message, A gen_server sends the message to all processes under that Room ID.

so far everything works great but the problem is:

When the user subscribes to the server, The server sends a message to all pids to notify subscribers about a new subscriber, however I cannot think of/know a practical way to do reverse of it.

I need to let people know that a subscriber has unsubscribed and remove the pid from the table. How can I implement such supervsor in yaws?


Solution

  • When the server sends a message to all pids to notify them of a new subscriber, have it also notify a process whose job is to erlang:monitor/2 each new subscriber process. That process, a gen_server, could keep a table of some sort as its state, like a map, dict, or ets table, that stores the reference returned from erlang:monitor/2 along with the associated room ID. When a subscriber process dies, the monitor process will receive a {'DOWN', MonitorRef, Type, Object, Info} message via its gen_server:handle_info/2 function, and it can then look up the MonitorRef in its state and notify the associated room of the dropped subscription.