t0mh

t0mh

Waiting for internet connectivity with Nerves

I’m building a Nerves application that requires a handshake on boot (or close to) over the network, what’s the best way to wait for a Network connection before attempting to make a network request?

I can see from the VintageNet docs you can look up the status of a specific interface, but the application will be deployed onto devices with eth0 or wlan0 connections - is there a way to generalise this lookup?

Also, any direction on how to implement this would be appreciated, I’m still pretty new to using Elixir and getting my head round recursion. For example, is it best to block to block while a network connection is found or to run a separate process that waits in the background and then alerts other processes to a working connection? I suspect for my use case it would be best to block until we have a connection, but I’m wary this might not be the “Elixir” way of doing things.

Many thanks.

Most Liked

fhunleth

fhunleth

Co-author of Nerves

To add to the comment about subscribing to vintage_net events, quite a few libraries choose to repeatedly try to connect whenever they’re disconnected from a server. For example, MQTT client libraries like Tortoise and the Phoenix channel client libraries like PhoenixClient and phoenix_gen_socket_client take this approach.

Losing a connection to a server isn’t a Nerves-specific issue. One advantage to polling is that your code doesn’t carry a dependency on vintage_net and therefore can be developed and run outside of a Nerves device.

The disadvantage of polling is that it can take your code a long time to connect to the server after it boots. A way around this is to register for interface up notifications from vintage_net and have those events “kick” your code to retry connecting to the server immediately.

Regarding your question about using a dynamic supervisor, I feel like that there might be some nuance in your use case that pushes you to one solution or another. You’ll have a GenServer that tries to connect. Once it connects, though, it could either hand off connection session to another GenServer or manage it itself. Some people like the state machine aspect of doing this in one place, but there’s a point where I think the code gets too complicated.

Where Next?

Popular in Questions Top

_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
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
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
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

Other popular topics Top

New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
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
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
New

We're in Beta

About us Mission Statement