ketupia
The LiveView in the tutorial runs a message loop every 50ms to load the player info and update the view to the current state. Has anyone tried using PubSub when the components are updated (thus relieving the need to poll)?
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app?
Looking for hints regarding:
Addi...
New
Kia ora,
We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
Hi all, I wanted to ask how the community is dealing with post-release steps.
Today we have Ecto migrations, which make sure that the db...
New
Hello,
I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #ai
- #genstage
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










Showing Posts 1 to 7- Show Best Posts
- Show All Posts (oldest first)
- Show All Posts (newest first)
RomanKotov
Hi,
Sorry, I did not look at the LiveView tutorial.
I have worked with polling model for music player a couple of years ago: local_assistant/lib/local_assistant/player/player.ex at f02e6779d011a612a317d41a97aad4c330a7034d · RomanKotov/local_assistant · GitHub
This was a local player. We used it in the office to play tracks.
Pros:
Cons:
I took the issues into account and created another application to play music, and some other stuff (it will be Elixir-based Smart Home). Now it uses PubSub (indirectly) and the other player (mpv).
New examples:
I like how newer approach. It works better
Have streamed the implementation process some time. Here is a thread about the Exshome: Exshome - DIY Elixir Smart Home
Hope this helps
RomanKotov
Sorry, have just noticed the context. I thought you were talking about the music player, but it was about ECSx player demo.
I have discussed ECSx topic on Twitter yesterday: https://x.com/kotovr/status/1753315848389865901?s=20
This thread contains ideas of how to use PubSub for ECSx, and it is very closely related to the approach I have described in the previous message with music player.
RomanKotov
I have researched ECSx yesterday. Will give my thoughts about version 0.5 (GitHub - ecsx-framework/ECSx at 7520dc6bd4837ad36d60f2d54942e868626fa365 · GitHub) - everything may change in future.
Manager starts :ets tables for each component. It also periodically runs code for each system. So, you have a single process, that runs the logic of your game. If there is an error in one system - it will crash the manager. The more time a system takes to recompute - the lower FPS you will have. I think it is better not to do heavy computations in any system. Possibly you can offload the computations to some task and then send results back via ClientEvents.
Just want to emphasize, that it is better not to overload your systems and keep them as lightweight as possible.
You can broadcast changes directly from your system. But sending messages will also take some time. The sending process needs to copy a message to mailbox of recipient (The BEAM Book: Understanding the Erlang Runtime System). It is OK, if you send only a small number of messages. But since you will have many components, the main loop may only work on broadcasting instead of useful work.
So, I think it is better to start with polling and see how it will work. Each polling process reads directly from :ets table, so it should not be a problem.
If you really want to use PubSub, then I think it will be better not to broadcast everything from the main game loop. You can start own processes, which will poll the required state and then broadcast it to the subscribers. For example, if you have a player (entity), you can launch a process for a player. It will periodically poll player components and then broadcast the data to every subscriber. For example, if your page shows 3 players - you need to subscribe only for them.
APB9785
Some good insights from @RomanKotov - particularly
is very important. Remember that reading from ETS (which we use for component storage) is extremely fast and lightweight - so if your goal is to reduce the number of times you are fetching the component data, it might not be a worthwhile goal. Better to save that type of optimization for later if you notice performance becoming a problem (and even then, it’s likely there are other things you could optimize that would make a greater impact)
RomanKotov
I have prepared some examples of how it is possible to use PubSub. I have just created a simple .exs file (for example
pub_sub.exs) and then ran it asiex pub_sub.exs.It does not use real LiveView, or ECSx, but shows some concepts:
Let’s break it down:
Mix.install(...)part installs all necessary dependencies.HPComponentandManaComponentemulate components from ECS.Entitymodule creates a behaviour for entities. As of 0.5.1 - there is no thing in ECSx related to the entities. Entity is only represented as id. So just thought it can be fine to gather every part related to this id from relevant components here. Behaviour needsget_value/1function. You can extract behaviour itself to other module, so it will not influence the compilation graph. I could add some__using__macros, but thought behaviour is enough here.Entitymodule also supports extra functions.get_value/2returns the current value for the entity. It is up to you whether to keep it.subscribe/2subscribes to the changes in Entity and returns its current value.broadcast_value/3incapsulates PubSub details. I find this approach useful in tests - you don’t need to start the whole system, you can just broadcast relevant data.PlayerEntity- implementsEntitybehaviour. Example of the entity. It takes the data directly from components (ETS in case of ECSx) and has no internal state. If you need multiple parts of player data in one place, possibly it is fine to call get_value from such entity. If you need only a couple of components - it is better to call them directly. You may also want to use structs instead of map here.EntityServermodule is a simple GenServer, that periodically polls the state of the entity and broadcasts changes (if there were any).LiveViewPlayersViewmodule is also GenServer that emulates subscription to the Entity changes.IO.inspect/1parts from it may be replaced with assign/2 or assign/3 in case of single objects. It also can be replaced with stream/4 and related API (if you want to render lists of items). There are some other techniques, but they are LiveView-specific.childrenstarts all necessary dependencies for this demo to work. They are PubSub itself (with all necessary Registries), a couple of Player entities (needed to use this syntax to launch multiple instances) and LiveView emulation process. Starting EntityServer instances may be delegated to own supervisor instead.Process.sleep(:infinity)line just for demo purposes here. It allows script to keep working (and printing changes in player state).I have also thought about the architecture of manager. Since all logic runs in one process, you may want to give higher priority for this process too (so less relevant ones will not interfere with critical computations). You can do this by updating priority in startup/0 function in your manager, like
Process.flag(:priority, :high). Possibly there some other options to do this.benwilson512
Be extremely careful with this. High priority processes that are also very busy can starve out other important functions like IO.
RomanKotov
Thanks for the tip. Yes, you are right - overusing this feature may lead to unexpected results, so it is better to use it with caution.
Right now ECSx (as of 0.5.1) uses only one global process (Manager) that manages all game logic. It periodically runs all mutations (Systems) and updates values in ETS tables for all related Components. The slower it recomputes all those values, the lower will be FPS and overall responsiveness of the game.
I considered raising the priority of the manager, because it is only a single process and it may be critical to the whole application. I would not recommend to update the priority, if system had more than one manager, or VM will have only one CPU (or scheduler).