shansiddiqui94

shansiddiqui94

PubSub vs LiveView Whats the difference

Happy Friday,

Phoenix LiveView is a powerful library that allows for real-time user experience updates. Meanwhile PubSub claims to publish and broadcast messages to a topic.

My question is why do we need both? why not just use LiveView and disregard PubSub. Can you give me scenario where PubSub and LiveView can work together?

Thank You

Marked As Solved

dorgan

dorgan

PubSub is like, well, PubSub. It’s the Publish-Subscribe pattern. In JS I think there’s the EventBus thing which is similar?

Processes subscribe to a topic they are interested in, like "user:541:notifications"
A publisher is someone that sends a message to that topic, and what the PubSub server(a message bus) does is to relay the message to every process subscribed to that topic.

You can read more about that here: Publish–subscribe pattern - Wikipedia

So, the publisher sends messages to the PubSub indicating the topic, and the PubSub takes care of figuring out who are the recipients and relays the message to them. The Publisher doesn’t need to know anything about the subcribers, it only sends messages to the PubSub server.

Channels and Liveview use this under the hood to relay messages to the right components. Channel topics in Phoenix are essentially PubSub topics.

Now Liveview is a complete different beast and it’s about building interactive user interfaces. It is built on top of Phoenix Channels, so by extension it also uses PubSub under the hood.

Because PubSub is a component implementing a pattern, you can use it for your own purposes, not only Liveview. That’s the example Steve was mentioning.

For a real world example you can check the LiveBeats app by Chris McCord. Here a liveview is subscribing to pubsub events:

And here are some functions that handle the messages coming through PubSub to update the UI or the liveview state:

Also Liked

sb8244

sb8244

Author of Real-Time Phoenix

Let’s start with the basics, you could use LiveView without PubSub. Phoenix has a dependency in the underlying channel in order to provide some convenience, but that’s not core to how LiveView/Channels work. Digging in:

On the surface LiveView lets you write (mostly) Elixir to build rich UIs. The messaging that happens back/forth to the frontend/backend is not tied to PubSub. For example, clicking a button will send a message from client->server but PubSub is not involved.

But LiveView isn’t just a library to let you build rich UIs in Elixir. It has a real-time component that lets you do things like update the UI based on a change that happened somewhere else. This is “magical” in some ways, but is critically backed by PubSub.

Let’s say that user A is connected to server 1. An event happens over a webhook and arrives on server 2. User A is not connected to server 2, so it wouldn’t get the real-time update and your UI is stale (not real-time).

This is where PubSub comes in. When the event happens on server 2, you can broadcast that event over PubSub. Other servers receive that event and they see that there’s a listener that cares about the event (your LiveView process). The event is processed and the UI is updated—your app is real-time.

Just to be clear though, that’s optional. If you just want the rich UI building and worry about real-time later, then you likely won’t even think about PubSub other than the setup instructions.

edit1: Added bit about how Phoenix has a dependency on PubSub, so it’s required to use LiveView. Added bit about how you don’t need to worry about PubSub unless you want real-time updates

derek-zhou

derek-zhou

May I suggest you to read a book? Asking on a forum works best if you already have the right context and only need a nudge in the right direction.

derek-zhou

derek-zhou

The author of real-time Phoenix, @sb8244 has answered your question. So maybe you can start with that book.

Where Next?

Popular in Questions Top

New
nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> somethi...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call t...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New

Other popular topics Top

malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 126479 1222
New

We're in Beta

About us Mission Statement