julismz

julismz

Hi all!

I’m trying to subscribe to a PubSub on a different module when I run the task which is gonna broadcast.

In the first place, I call an async task, which calls a function on a specific GenServer. That job could take 10 seconds.

I want to allow user to keep navigating the site and subscribe everywhere to that topic, waiting for the PubSub reply message.

If I stay and wait, I receive the message with the handle_info, but if I change the page, even subscribed again, the broadcast is lost on the space…

To my surprise, seems like “broadcast” only send the message to a specific process instead “broadcast” to all the process…

I’m thinking on face this with WebSockets, but I thought that PubSub maybe has something for these “persist” cases… SQS or MQ could be another option but I think that is too much…

Any suggestion?

Showing Posts 1 to 10

kokolegorille

kokolegorille

You can broadcast on a channel, and receive on another one…

You can do it per user, or per session.

As long as users have their own custom channels, it’s possible to use it for reply. If You subscribe to this channel in each liveview, that should be fine.

julismz

julismz OP

Is what I’m trying to do… My channel / topic is called “blockchain”.

On my liveview mount()

 if connected?(socket) do
      Phoenix.PubSub.subscribe(App.PubSub, @topic)
 end

On my GenServer:

Phoenix.PubSub.broadcast(App.PubSub, @topic, %{
          status: :error,
          message: "Error detail"
})

On my liveview:

@impl true
  def handle_info(message, socket) do
    IO.puts("Message received!!!")
    case message do
      %{message: error, status: :error} ->
        {:noreply,
         push_event(socket, "transaction_finished", %{status: message[:status], message: error})}

      %{tx: tx, status: :sent} ->
        {:noreply, push_event(socket, "transaction_finished", %{status: message[:status], tx: tx})}

      _ ->
        {:noreply,
         push_event(socket, "transaction_finished", %{status: :error, message: "Error undefined"})}
    end
  end

If I keep waiting, I receive the message. If I change the view, I get nothing…

kokolegorille

kokolegorille

You also need another one…

Phoenix.PubSub.subscribe(App.PubSub, "session:#{session_id}")

You can generate a random session_id and use it as a personal channel, where You receive anything personal.

You might use user instead, if your users needs to be authentified.

julismz

julismz OP

Ok… so now I have this:

 def mount(_params, session, socket) do
    user = Accounts.get_user_by_session_token(session["user_token"])
    
    if connected?(socket) do
      IO.puts("Subscribing to user:#{user.id}")
      Phoenix.PubSub.subscribe(Coopchain.PubSub, @topic)
      Phoenix.PubSub.subscribe(Coopchain.PubSub, "user:#{user.id}")
    end
    ....
end

And on the GenServer side I should broadcast to…?

 Phoenix.PubSub.broadcast(App.PubSub, "user:#{user_id}", %{
          status: :error,
          message: "Error on abi parser"
        })

I’m not sure about following you… Thanks for your patience…

julismz

julismz OP

This is the flow:

1)I subscribe on liveview.
2)I send a job to GenServer using Task Supervisor async.
3)I move to another view when I’m also subscribed.
4)I receive the PubSub message there.

broadcast is only on GenServer… The first job is triggered by a Task:

Task.Supervisor.async(App.TaskSupervisor, fn ->
      params = {
        user_id,
        coop_id,
        data.title,
        data.file,
        data.hash
      }
end)
      subscribe_to_contract(params) #This function call GenServer async.
kokolegorille

kokolegorille

Something like this… the main part is You will connect to this custom channels in all your liveviews.

julismz

julismz OP

Is what I do… but message does not arrives… I got Client #PID<0.1240.0> is dead

kokolegorille

kokolegorille

Using Task Supervisor async will link the caller, and that is not good if the caller is the liveview process… because it will die when the liveview process dies.

julismz

julismz OP

Seems like RabbitMQ or SQS is my only alternative here… so bad…

kokolegorille

kokolegorille

There are more tools than just Task async… for example, having a manager gen_server being the middle-man.

When You call your job, You do it through a GenServer, that does the call to the worker. Then, the liveview can die, but the worker won’t.

You might look at OTP tools first, before RabbitMQ and SQS. There are many, and the Task module is one I use when I don’t need to control the process.

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews