Building Realtime LiveView App using Phoenix PubSub

Search for a command to run...

No comments yet. Be the first to comment.
Motivation Recently, I tried to learn some low-level system programming stuff. I am a Mac user, and I thought that everything that works on Linux should also work on Mac. After all, Mac is a Unix-based system 😊. I guess we all heard this. Oh boy! I ...

Problem Statement While working on a Nestjs project, I encountered a weird problem related to the database column. I was trying to insert a record into a MySQL table using TypeORM. The error I was experiencing stated that a specific column “cannot be...

Recently, I was exploring design patterns courses on my LinkedIn Learning subscription. I came across a course, Node.js: Design Patterns by Alex Banks. It is a wonderful, easy-to-understand course. I started with the Builder Pattern, and the explanat...

Suppose you are working on a table in a LiveView project. This table has limited static data of not more than one page (you can avoid questions about pagination in the comment section 😊). From a user's point of view, it becomes hard to look into the...

Recently, while working on one of my personal Elixir projects. I came across a scenario where I needed to make some changes to the database schema.The changes mainly revolve around adding and removing indexes due to changes in the business requiremen...

In my previous blog on the Note-taking app, we missed harnessing the most important feature of Phoenix LiveView i.e real-time update. What does that mean? It means to do an update in your application screen without explicitly refreshing your application. For example, in a chat app, you don't have to explicitly refresh your screen to see the change in the screen. In our previous version of the Notetaking app when we add the note in the screen and if there is a browser screen opened parallel with the existing one then that new note won't be reflected, you have to refresh the browser to see the result. In this blog, we are going to add this new feature to our previous blog code.
Refer to the gif below for the complete real-time update version of the app that we are going to make.

Phoenix PubSub is an API that is responsible for doing live updates in the app. The documentation itself says it's a Realtime Publisher/Subscriber service. In application, our LiveView(module) process subscribes to a particular channel(s). When a message is published on a topic, the message is broadcast. Similarly, when a message is published from a topic, the message is broadcast to all the subscribers of that topic. When the broadcast message is received to the subscribed Process their state is updated which ultimately re-renders the view. Below diagram will give you an idea about subscribers and


Phoenix.PubSub module has a subscribe function that takes 2 parameters. NoteApp.PubSublib/note_app/notes/note.ex and the following code.defmodule NoteApp.Notes.Note do
def subscribe() do
Phoenix.PubSub.subscribe(NoteApp.PubSub, "notes")
end
end
subscribe function, likewise, it also has a broadcast function in it.publish/broadcast the message.create_note callback just after creating the note.message name(as an atom) in our case it will be :create_note and the other field is the note created.lib/note_app/notes/note_server.ex
def handle_cast({:create_note, note}, notes) do
updated_note = add_id(notes, note)
updates_notes = [updated_note | notes]
message = {:note_created, updated_note}
#broadcast logic -start
Phoenix.PubSub.broadcast(
NoteApp.PubSub,
"notes",
message
)
#broadcast logic -end
{:noreply, updates_notes}
end
broadcast function takes 3 parameters.NoteApp.PubSubmessage we want to broadcast.notes topic.NotesLists LiveView needs to subscribe to this topic/channel.subscribe function in the mount callback.subscribe function when our LiveView is connected to socket. Also, we need to alias the Note module to use
lib/note_app_web/live/notes_index_live.exdefmodule NoteAppWeb.NotesIndexLive do
alias NoteApp.Notes.Note
def mount(_params, _session, socket) do
if connected?(socket), do: Note.subscribe()
.....
.....
.....
end
end
handle_info callback in our NotesList LiveView module. Add the following function in the module.lib/note_app_web/live/notes_index_live.ex
defmodule NoteAppWeb.NotesIndexLive do
........
........
........
def handle_info({:note_created, note}, socket) do
socket = update(socket, :notes, fn notes -> [note | notes] end)
{:noreply, socket}
end
end
broadcast message and retrieve the note and update the notes list in the socket with the latest message.I hope you like this easy implementation of using Phoenix PubSub for achieving live updates. If you have any questions then please comment below.