josefrichter

josefrichter

Stream pagination and subscribe/unsubscribe to individual items

Hi,

I am paginating through a bunch of classes, displaying 10 at a time. Every time I change a page, I need to subscribe to the new classes count, and unsubscribe from those shown previously. (I want the count to live-update in the table, so I will be re-inserting the classes to the stream when update is broadcast)

The only thing I was able to make work is

unsubscribe (kinda workaround)>

Sorted.PubSub
    |> Registry.keys(self())
    |> Enum.map(fn topic ->
      Phoenix.PubSub.unsubscribe(Sorted.PubSub, topic)
    end)

and subscribe>

class_ids =
        socket.assigns.streams.classes.inserts
        # Convert stream to list eagerly
        |> Enum.to_list()
        # Extract IDs
        |> Enum.map(fn {_dom_id, _, class, _} -> class.id end)

      class_ids |> Enum.each(&Sorted.Pubsub.subscribe_class_count/1)

It works, but it kinda smells to me…

I was hoping I could do something like this for both unsubscribe and subscribe

for {_dom_id, class} <- socket.assigns.streams.classes do
        Sorted.Pubsub.subscribe_class(class.id)
      end

but this is throwing

** (ArgumentError) streams can only be consumed directly by a for comprehension.
If you are attempting to consume the stream ahead of time, such as with
`Enum.with_index(@streams.classes)`, you need to place the relevant information
within the stream items instead.

On top of that, even with the above subscribe code, I was not able to unsubscribe before changing the page, as the list was always empty..

So what’s the proper way to go about this? Thank you very much.

Most Liked

codeanpeace

codeanpeace

My rule of thumb for streams is large collections that are ideally more “static” in nature and don’t require too much intricate interaction with the server. It’s especially great for collections that keep on growing over time – logs, users in chatroom, etc. If you’re only displaying 10 at a time, streams may be overkill since it does add some complexity while reducing memory usage.

That said, bouncing off of what @LostKobrakai said, you could also track the bare minimum necessary for pubsub subscribe/unsubscribe as an assign e.g. current_class_ids and use that instead of reaching into the registry and streams. ¯\_(ツ)_/¯

Schultzer

Schultzer

Making your subscription to granular can be problematic for several reasons, my thumb rule for building a complex real-time database management for Ecto is to subscribe to a table.

I should have made it clear, that I use stream, pubsub, counting and updating rows, logs etc.

Where Next?

Popular in Questions Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
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
dotdotdotPaul
Okay, I’m having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I’m sure I’...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
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
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New

Other popular topics Top

New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
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 39467 209
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
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
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
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

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement