Qqwy
I am working on a small library that computes a moving average, which would work well with Phoenix PubSub, in that it could subscribe itself to a topic using Phoenix PubSub, and then handle the events as they come in.
However, users should be able to modify the exact logic that is going on inside, including what topics it is subscribed to. This is a bit of a problem though, because a PubSub-broadcasted message does not contain any identifying information (It is just a ‘plain’ message that will trigger your GenServer’s handleinfo.)
So, if I want to give users the ability to subscribe to varying PubSub topics, they need to be able to tap into the handleinfo part of the GenServer that the library constructs, to allow matching on the proper messages.
What would be a proper way to do this?
Trending in Questions
Other Trending Topics
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










First 10 of 15 Posts
idi527
I remember seeing GitHub - JEG2/hanabi_umbrella: The sample application for Paul Dawson and James Gray's Lonestar Elixir 2017 talk: The NeverEnding Application. · GitHub, where I think phoenix pubsub was used to tie different umbrella apps together. You might want to look into
hanabi_engine/game_manager.ex.Maybe you can use
pg2without phoenix pubsub in order to add your custom logic?OvermindDL1
Really? I thought a PubSub-broadcasted message was contained in a map/struct that contained all kinds of information such as the event, pubsub topic, data/payload, etc…?
What are you trying to do? Say via pseudo-code or so?
Azolo
Sure, as long as the messages are sent through Phoenix.
Basically Phoenix, not PhoenixPubSub is what adds that information.
That being said, Phoenix is probably the one of the best examples on “How to implement application PubSub messaging.”
chrismccord
I don’t have a full picture of your use case form your description, can you describe a little more what you mean by:
Maybe showing a pseudo client/server api that you’re thinking of would help. What I can say is if you need certain info to be a part of the message, you can make your own message contract that includes this info, just like the Phoenix.SocketBroadcast for channels contains extra info, your contract could be
{group, event, term}, for example:Make sense?
Qqwy
Thank you very much for the wonderful replies thus far.
Let me try to explain in a little more detail what I am trying to build:
The end result of what I want to make, is a library that facilitates the on-line calculation of sums, products and averages that can be used to draw pretty graphs and summaries.
In an application I am making (a kind of game), players can make events happen. These events, are the buying/selling of in-game resources. So each event, or transaction, can be considered a struct like this:
%Transaction{resource_id: integer, n_resource: integer, money: integer, created_at: DateTime.t}. For buying,n_resourceis positive whilemoneyis negative. For selling, the opposite.Now, I want to keep track of the average buy/selling price of the different items, as well as the amount of items bought/sold in total during this time. Not only the ‘current’ average, but a moving average over the past minute, fifteen minutes, hour, day, week (the different window types).
For each
{resource_id, window_type}, I thus want to start a GenServer that receives the desired%Transaction{}s, keeps N queues, where each queue contains all items whosecreated_atis between the beginning and end of the window.So for instance, the window size might be
15 minutes, and I might want to keep track of the scores in the last3615-minute intervals. Thus, there are 36 queues.When a new item arrives, it is placed at the end of the newest queue.
Every few seconds, items from the front of the oldest queue are removed until the front item is newer than its cutoff. This is repeated for the one-before-oldest queue, but instead of removing these items, they are placed at the end of the oldest queue. This procedure is then repeated until all queues only again contain items part of the time window with relation to ‘now’.
As items are added and removed to the queues one-by-one, we can calculate sums and products (or any other commutative reversible operation) and averages (one sum divided by another sum) on-line:
As this is an on-line procedure, at any given time it will probably be very fast to update any of the GenServer’s states.
This seems like the kind of thing that would be very useful in its own library.
However, making sure that it is easy but customizeable to send events (like, in this case, new
%Transaction{}structs) to the GenServer is what I am stuck at. I would like to use Phoenix.PubSub, but would not:Phoenix; the library should only depend on the publish/subscribe functionality.Azolo
1.) What is it exactly that makes you feel like you can’t use
Phoenix.PubSuboutside ofPhoenix?2.) I don’t understand your point here. You are in control of what you broadcast. If you want want more information in your broadcasted messages then you have to broadcast that information.
Phoenixbuilds aBroadcaststruct for every broadcasted message.Qqwy
@Azolo: The first point is a reply to what you yourself have said in reply to @OvermindDL1:
So if you use
Phoenix.PubSubdirectly, you do not get a%Broadcast{}.The second point: While I am in control of what I broadcast when building this thing straight into my application, I would like to extract it into a separate library.
When it is a library, I am no longer in control of what the library-user broadcasts to the library’s GenServer. I would like to give users of the library the ability to handle arbitrary messages that are sent to the GenServer, but preferably without them needing to wade into the inner
defhandleinfo-guts of the GenServer.So to summarize: I’m not really sure how to, when extracting this functionality into a Library, enable the user to do proper handling of messages received at the Library-spawned GenServer.
Azolo
I see, I consider that to be a good thing. When I used
Phoenix.PubSubI didn’t have a need for theeventfield. So theBroadcaststruct is aPhoeniximplementation detail.Well, you could broadcast a separate message for your tracking. Other than that the scenario that you’re describing sounds like my brother asking me to pick a number and me responding with banana. There’s not much recourse without forcing a contract on data coming out of your library.
OvermindDL1
They are basically no different from normal messages, so that then begs the question: How do you handle arbitrary messages sent to your process (any process, pubsub or not)?
Also I’m curious what you mean by this? An
infomessage is just a non-genserver-wrapped message, very very common in GenServers?chrismccord
I still don’t have a complete picture, especially around users making their own events, but let’s start wit this code. Here we have a
GameStorewhich is the boundary for the game system. You are correct that you want to wrap the pubsub mechanism so the caller doesn’t need to know about it. Given different actions in the system, like purchasing or transferring an item, you’ll broadcast events as part of the success cases. Meanwhile, you can expose asubscribe/0,subscribe/1and unsubscribe functions on theGameStorefor other callers to consume. If they are interested in stats they can subscribe to the “stats” group. I show the Caller example below the GameStore:Does this put you on the right track?