Notifications with phoenix

Does anyone know of any documentation to know how notifications are handled in Phoenix?

what kind of notifications?

internal pub/sub?
postgres/db notifications?
ios/android notifications?
browser notifications?

or phoenix “flash”

2 Likes

these:

phoenix flash: https://hexdocs.pm/phoenix/search.html?q=flash - first link

browser web push is a bit more complex… look at https://github.com/danhper/elixir-web-push-encryption and the client sample https://github.com/danhper/elixir-web-push-encryption/tree/master/client-sample

Have a look at the default layout file (app.html.eex) located in

lib
├── app_web
│   │   ├── layout
│   │   │   └── app.html.eex

you will find a section with the following information.

    <main role="main">
      <p class="alert alert-info" role="alert"><%= get_flash(@conn, :info) %></p>
      <p class="alert alert-danger" role="alert"><%= get_flash(@conn, :error) %></p>
      <%= render @view_module, @view_template, assigns %>
       .................

the paragraph with the alert tags are what shows the flash messages, you can give it any class that matches the styling you want depending on your CSS

2 Likes

hi. I need some ideas on how to handle postgres/db notifications?

I think he was only providing choices for the OP to open up their question and narrow it down to help them.

You don’t get notifications from postgres/db since it is a synchronous operation. You execute a command, operation and you will receive a result back.

It is up to you how to handle those results

ie:

case Db.Driver.ExecuteQuery(my_query) do
  {:success, result} -> show_success_message(result) 
  {:error, message} -> show_error_message(message)
end

If you are using an ORM like Ecto then the methods for operations and their result types are already defined to help you in their documentation pages.

Hope this helps a bit to your quest.

@Maxtonx

Checkout Phoenix PubSub - a distributed realtime Publisher/Subscriber service:

  • broadcast event with data in your contexts
  • subscribe event and handle the event as you need.

You can also check these articles…

https://blog.lelonek.me/listen-and-notify-postgresql-commands-in-elixir-187c49597851

https://medium.com/@kaisersly/postgrex-notifications-759574f5796e

I use GitHub - bitgamma/boltun: Transforms notifications from the Postgres LISTEN/NOTIFY mechanism into callback execution for pg listener/notifications…

What is the best way to design a notification. Suppose I have a user and post relation. If someone comments on the post user should get a notification.

Is it good to create a notification table to track all the notification. I want to design this simple notification. Can you guide me something to start with