When ever a user connect to our phoenix channel like user join a channel when he login and leave channel when logout.So whenever it join channel we create Postgress notification process and listen to triggers
{:ok, pid} = Postgrex.Notifications.start_link(repo_config)
{:ok, ref} = Postgrex.Notifications.listen(pid, "#{@channel}#{Enum.at(args, 0)}")
When user logout or leave channel due to some other reason like close browser we unlisten and stop the process.
Postgrex.Notifications.unlisten(state.pid, state.ref)
:gen_server.stop(state.pid)
Is This good Approach.
Other Approaches in my mind are.
- Create single Listener Genserver then broadcast to respective user.(Reason may create
bottleneck ) - Create pool of Genserver and main listener Genserver just receive from Postgres and send to other Genserver from pool which then do some processing and broadcast to user channel.(Processing involve some data manipulation not any heavy task just add and check some field in user struct)