Nefcairon
Hello,
Still new to Phoenix I wonder what’s the best way to do pagination (or even better infinite scrolling).
Can you recommend me a library that works with Phoenix 1.4-RC3 (which uses Ecto 3 if I am correct) and is easy enough to use for a beginner? Is there a de-facto standard library in the elixir world?
I hope you do not mind such questions.
Thanks!
Trending in Questions
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
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
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
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
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
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
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
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
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
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 6- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
Eiji
@Nefcairon My favorite way is to use Repo.stream/2 in
Websocketconnection (see Channels guide). Since you decide to useWebSocketyou will have 2-way communication which means you can query anything and return results one by one in really small and fast messages whenever you want to. This solution is also really well scalable. No matter if you have100or100_000_000entries returned from query you still only fetch and sends exactly same count of entries at a time (just only number of server messages is changed).If you want to paginate query you can use
ecto’s Query API, but before you decide to use Ecto.Query.limit/3 make sure you have read why it’s considered as bad practice in some cases:For more information please see What is the best approach for fetching large amount of records from postgresql with ecto topic.
LostKobrakai
For pagination where I need to show page numbers I usually use scrivener_ecto. For infinite scrolling I use GitHub - duffelhq/paginator: Cursor-based pagination for Elixir Ecto · GitHub.
ibarch
Could you please explain this approach in further details for 100 million records?
Eiji
Number does not matter since you are using
Stream.Here we have few assumptions:
WebSocket(or other bi-directional communication way) is required as well asPhoenixchannel API (or equivalent)Channelwe can assign any data for eachWebSocketconnectionWebSocketconnection have it’s own processFrom this we can create scenario like:
Client request
GET /postsServer sends basic
HTMLDOM with aJavaScriptcode/postsis list so we are automatically appendingtableelementClient is requesting all
Postfrom databaseServer is generating per-client query id (for simplicity let’s say it’s counter) - example return:
{"id": "query1", "status": "OK"}(which means that query with id1is valid and therefore will be send).Client is setting to
tablepropertyidwith valuequery1and propertydata-modelwith valuepostServer is calling Ecto.Repo.stream/2 in background and in Stream.each/2 it’s sending data like:
{data: {…}, "id": "query1", "type": "append"}.Note: You can optionally use Stream.chunk_every/2 before Stream.each/2 if you want to send more than one database row at a time.
Client receives such response and it’s creating
trDOM element for it with id:query1-#{resonse.data.id}If there is created, deleted or updated any
Postthen server could send something like:{data: {…}, "model": "post", type: "delete|update"}Client accepts such event and it’s looking for
table[data-model="post"] > tbody > tr[id^="query"][id$="-#{response.data.id}"].With that we are sending always same amount of data for each
clientrequest at a time. Later it’s possible to limit number of requests per client (usingChannelassigns).ibarch
Thank you for such a clear answer.
I see that you’re mixing pagination with live updates in order to provide a single convenient interface for a client via Channels. This approach and transport are great, but I’m wondering whether Repo.stream/2 is suited for pagination?
If I got it right, Repo.stream/2 uses database’s internal cursors to efficiently select millions of records one by one or in chunks, but at the price of taking up a connection from the pool till it’s done. So the more records and clients you have, the faster you run out of available connections. Since clients don’t need all those millions of records, you have to apply a limit with Enum.take/2 and keep the last record’s unique identifier AKA external cursor (typically a combination of ID + timestamp) on the client side or in the Channel’s state for the next page request.
A plain query with Query.limit/3 is simpler and cheaper than Repo.stream/2 for this case, isn’t it?
Eiji
It’s why I said it’s example scenario. Instead of sending one row right after another you can request 30 rows at a time and wait until
clientsends request like:{"id": "query1", "type": "next", "value": 30}and then you just need to send back next 30 rows etc. There is lots of scenarios depends on use case which could be created using such API.