favetelinguis
I have a bunch of IOT devices generating event to my Elixir app. Each IOT device has its own topic/channel in Hub. What I would like to build is a monitor GUI where I have a list of all the IOT devices I can click and then I subscribe on all events from that device, I can click on another device and then I should unsubscribe from the old device and subscribe to the clicked device. I see Drab as a perfect fit for this but im not able to get it to work and im not sure what is the best way to design my code for this use case. This is what I have so far.
defmodule BfgWebWeb.PageController do
use BfgWebWeb, :controller
def index(conn, _params) do
market_ids = BfgEngine.MarketsServer.list_market_ids()
render conn, "index.html", market_ids: market_ids, genevent: ""
end
end
defmodule BfgWebWeb.PageCommander do
use Drab.Commander
require Hub
require Logger
# onconnect :connected
defhandler subscribe_market(socket, _sender, market_id) when is_float(market_id) do
market_id = Float.to_string(market_id)
Logger.debug("Change subscription to #{market_id} is it string #{is_binary(market_id)}")
# TODO where can I save this sub so that I can use it to unsubscribe later Hub.unsubscribe(sub)
sub = Hub.subscribe(market_id, _)
handle_event(socket)
end
# def connected(socket) do
# sub = Hub.subscribe("market_id", _)
# Logger.debug("Connected and subscribed to market_id #{inspect sub}")
# handle_event(socket)
# end
defp handle_event(socket) do
receive do
msg ->
Logger.warn("Got msg #{inspect msg}")
{:ok, events} = Drab.Live.peek(socket, :genevent)
socket
|> poke(genevent: inspect(msg) <> "\n" <> events)
end
handle_event(socket)
end
end
<div class="page-header">
<h1>Select market</h1>
<div class="btn-group" role="group" aria-label="...">
<%= for market_id <- @market_ids do %>
<button type="button" class="btn btn-default" drab-click="subscribe_market(<%= market_id %>)"><%= market_id %></button>
<% end %>
</div>
<pre><code class="accesslog"><%= @genevent %></code></pre>
</div>
My app is starting and im getting no errors but im getting no messages. There are some issues I need to solve here:
- Get this example working.
- When I hit another button I should unsubscribe from the old subscription.
- I should only be able to have one subscription going at any one time.
Any ideas on design/bug hunt of this would be much appreciated.
Trending in Questions
I’m aware of 2 such projects, are there others?
git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Hi everyone.
My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
Hello folks!
So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
How Can I Optimise Compile Time Dependencies
I have been building an elixir application for about 2 years now. Many modules and files ha...
New
Hi,
we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
Other Trending Topics
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with.
But I don’t use it anymore.
Serverless was the promis...
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
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #performance











Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
grych
That could be a tricky part. Are “market_ids” float? Actually I’ve never tested handlers with guards. I am using
apply/3to run the handler. Hmm, need to try it.Could you please remove the guard and paste the exact output? Is it showing “Change subscripiton to …” in the logs?
favetelinguis
Even with guard im getting the correct log message:
grych
So, from the Drab point-of-view everything works, you have “change subscription to ..” message. I understand that everything after
Hub.subscriptdoes not work.I would suggest checking what is returned by
Hub.subscribe. I understand it subscribes something toself(), which is received later. You may try to debug it in iex, without phoenix or drab involved.favetelinguis
I have Hub working in my core application. Im just trying to get the GUI layer working now.
How would you suggest I can switch subscriptions? To do that I ned to save the sub returned by
Hub.subscribeand the second problem is to cancel the one long running process I start when I hit a button and to start another one.favetelinguis
So I found the bug but not sure what to do about it, this is the issue:
"__additional_argument" => 1.14630004"text" =>"1.146300040", "html" => "1.146300040"This is wrong:
[debug] Change subscription to 1.14630004 is it string trueIt should say:
[debug] Change subscription to 1.146300040 is it string trueBut the additional argument cuts off, not sure what is going on since the argument shown in the button is the right on but the additional argument is one digit short.
grych
In your model, you start the handler function and do a
receiveforever. Fair enough, but to cancel this from the other process, you need to store the pid somewhere, and send the cancel message to it, or just kill it.Sorry, but without the knowledge of
HubI can’t tell more. Maybe it already got the unsubscribe api?favetelinguis
So am I understanding correct if I say drab is starting a new process (GenServer?) for each call I make to any
defhandler, and self() would be the pid of the process this call as spawned?grych
Welcome to the float hell. This is why you should never use floats in non-scientific applications
Especially when inter-operating between different languages.
I would suggest that you should send string to the backend, so instead of:
do:
Or multiple it by 10000000 and use integer.
That would save you a lot of sleepless nights
But the only real solution is to not to use floats. Never ever.
grych
True. Each handler runs in it’s own process. Otherwise you could push the only 1 button on the page in the same time
favetelinguis
Then its clear to me what I need to do
Thanks for the help!
Btw, The float is treated as a string in every place except here, somehow it gets infered as a float in the client.