Linuus

Linuus

So, I haven’t really worked with MQTT before and now I want to build a proof of concept project using it.

Some context:
We have many devices and today we are not using MQTT, but we might do in the future. Our API makes HTTP requests to devices. We don’t want the API itself to handle MQTT but keep doing HTTP requests.

So basically:
API --HTTP POST–> Elixir App --MQTT–> Broker --MQTT–> Device
and for state changes etc:
Device --MQTT–> Broker --MQTT–> Elixir App → API/Google PubSub or wherever it should be piped.

I have a basic setup now with a simple ESP that connects to a broker and an elixir app which spawns a Genserver (with emqtt) which connects to the broker and subscribes to some topics. This works fine for my single test device.

My feeling though is that it might be a bit limiting to have one Genserver handling all MQTT messages, right?

How does one normally handle this when you have thousands of devices? Spawn a Genserver per device which only handles that device’s messages?

Keep it at one Genserver but as soon as a message arrives, spawn a new process to handle it?

I guess it also depends on the frequency of messages per device as well, and each device won’t send a lot of messages. There might be times during the day where many devices send messages at about the same time, but each device won’t generally send messages at a high frequency.

Anyway, I know that it’s probably very application specific, but perhaps someone with more MQTT experience can give some more general tips and guidelines?

Showing Posts 1 to 3

D4no0

D4no0

Since you will most probably will not write the MQTT server yourself, you should take a look at existing libraries and if they fit your needs (for example I am not sure all of the existing libraries support MQTT3).

The part responsible for concurrency should be already implemented in the library in most cases, so I would say to first investigate existing libraries.

Linuus

Linuus OP

Thank you for your reply!

With MQTT server you mean the Broker? I have already picked EMQX and I’m using their Erlang mqtt client emqtt (it support MQTT5 as well, which I need since I want to test out the request/response pattern built into v5).

My test project so far is inspired by their guide MQTT for Elixir | EMQ and I have it connected already. I can publish/subscribe in my ESP8266 and I can publish/subscribe in my Elixir app :slight_smile:

Now, in their example they spawn a genserver, in which they connect to the broker and all messages arrive in that genserver as calls to handle_info.

So my question is mainly, is that a valid approach for larger fleets of devices or what should I keep in mind to make it scalable?

D4no0

D4no0

OK, it has been some time since I worked with embedded devices :joy:, the last time I worked on a embedded project v3 spec was recently out.

OK, this makes more sense.

Highly depends on what kind of QOS you are aiming to use. If you aim to use QOS 0, then you can most probably get away with a single genserver (as long as you don’t do the processing in that process too).

Since processes are cheap to spawn, I would recommend for the first implementation to go wild and handle as much as you can in separate processes. You can wire some telemetry at the same time and measure the actual throughput to determine if the solution is an overkill and optimize accordingly.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
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
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
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
velrest
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
samoloth
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
FlyingNoodle
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
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
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
Damirados
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews