Linuus

Linuus

Architecture For MQTT

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?

Most Liked

Linuus

Linuus

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?

Where Next?

Popular in Questions Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New

Other popular topics Top

TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41539 114
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement