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
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 7- Show Best Posts
- Show All (oldest first)
- Show All (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).