Aguxez

Aguxez

Displaying data from database in real time in Phoenix

I have a question about broadcasting data from Postgres. I understand the way that you could do this if you’re building for example a chat application using Javascript, etc, etc… but what I had in mind is some kind of counter that is updated when someone interacts with a specific bot, for example, from top of my mind, there’s a big number (Just 1 number) in the web page and someone sends a message to the bot saying that he wants to add 10 to that number then the number updates in real time when the transaction to the database is done, my question is. How can I update this from channels or something else? Do I still need the JavaScript to interact with he DOM in real time? Perhaps use something like RethinkDB? I’m pretty lost with the implementation in this one.

Marked As Solved

Qqwy

Qqwy

TypeCheck Core Team

I now am at a computer, so it is easier to mock out a code example.

This is what I have done lately for a proof-of-concept ‘online game’, where game state is regularly updated, and then pushed towards all connected players. It does not depend on a database, since updating the data usually does not depend on the database (unless another application connects directly with the database, in which case Boltun might be what you want).

In the module that maintains the player state, I run the following function when this is updated:

  defp broadcast_update(state) do
    Phoenix.PubSub.broadcast(OnlineGame.PubSub, "player_state_update:#{state.user_id}", {:player_state_update, state})
  end

(Note that OnlineGame.PubSub is the name configured for Phoenix Pubsub, see your Phoenix config file, it will by default be MyAppName.PubSub.)

This will send an update to any other process that would like to listen to the player_state_update:2 topic.
To subscribe a Phoenix Channel to this, which forwards the information over a Phoenix Channel to a user’s Browser, I have the following

code in the channel:

defmodule OnlineGame.Web.GameChannel do
  use Phoenix.Channel

  def join("player_state_update", _params, socket) do
    user_id = get_user_id_in_some_way()
    Phoenix.PubSub.subscribe(OnlineGame.PubSub, "player_state_update:#{user_id}")
    # Some other stuff not relevant for this example, and finally:
    {:ok, socket}
  end

  def handle_info({:player_state_update, player_state}, socket) do
    push socket, "player_state_update", player_state
    {:noreply, socket}
  end
end

So you just override the handle_info from another GenServer. Note that, since the process just receives the message without being given information about what topic it was the message arrived on, it makes sense to send a tuple like this.
The implementation of handle_info here just forwards the information to the socket.

The cool thing about this approach, is that my Game context does not need to know that I use Phoenix. (But just that I use Phoenix.PubSub). I could register to this update in any other location of the application.

Also Liked

yurko

yurko

If I understand correctly then you can use a GenServer with an optional PubSub abstraction to achieve that and use database for persistence without building the whole thing around the database.

Say you have a GenServer that loads its initial state (number) from the database and has a function that updates (or adds to) this number, this function can be used anywhere in the application.

To be able to notify other processes about changes, this GenServer can store a list of pids and send messages to them or use one of existing abstractions, registry would do good.

Web layer: the webpage should have a channel connected and subscribed to changes (in join function), it will then receive updates and can act accordingly (e.g. push changed number to the client where the DOM can be updated).

Finally the database: depending on the number of updates you could subscribe to the same GenServer and update the table on each change or (if that happens a lot) add another lightweight GenServer, it can fetch the state from the publisher onece every minute or every X seconds and update the database.

Qqwy

Qqwy

TypeCheck Core Team

Good news! The hard work to send updates to other places in your application is already done for you by Phoenix.PubSub. There is no need to re-build it yourself (storing a list of pids, handle pids disappearing and reappearing, etc).

Depending on the scope of your application, you might either allow the location that performs the update send a broadcast to the registered Phoennix channels directly, or add a layer of indirection by manually subscribing your channels to a manually made PubSub location, in which case the broadcasting location does not need to know anything about the existence of a web layer (and you might be able to reuse it later for other types of data pushing).

brightball

brightball

You can also use Boltun to take advantage of Postgres LISTEN/NOTIFY if you do want to build it around the DB.

Where Next?

Popular in Questions Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New

Other popular topics Top

lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31142 143
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36128 110
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New

We're in Beta

About us Mission Statement