1

This is not a specific coding question, but more of a best practices advice to a newcomer into the web apps world with years of experience in five-nines telecom software development.

I have an ASP.NET web application that collects data from several embedded (Arduino) devices. The device push data periodically via simple HTTP GET requests. Under normal conditions, each device pushes data once every 30 seconds. However, there is a chance that a device could be lost or connectivity from the device to the server could be lost. I need to detect this condition within 2-3 normal data reporting cycles.

If it was a typical stand-alone C# application, I would start a timer every time I receive data from a device with the expiration set to the maximum timeout duration. If the timer expires, this means I have not heard from the device for too long and I need to trigger an alert (email or something) from my application.

So, the inactivity timer approach seems to be pretty typical for stand-alone always-on applications with non-persistent connections. But what is the right way of doing this in an ASP.NET web app? Can I have timers of this kind running outside of my HTTP request handlers?

I searched for possible solutions, and people seem to have more questions about timing out browser sessions and such. My problem is different in these aspects:

  1. I have a very light-weight client incapable of session state tracking
  2. I need to detect inactivity in a timely fashion to alert/alarm the user, who is not interacting with the system until there is an alarm

Thank you for sharing the wisdom.

johna
  • 10,540
  • 14
  • 47
  • 72
evolvah
  • 625
  • 4
  • 15
  • by "push data" is the server hanging on to that data? if so, best practice is to use a post request, not get. get requests should be used to only get data – czifro Jul 27 '14 at 17:29

1 Answers1

1

My suggestion is first if you are using get to add data to the server, switch to post. Second, try using a token of some sort. The first request (get request) a user makes should be to an endpoint that they can get an access token. then every time they connect via post for pushing data, get the access token from the headers. check if the token has expired yet. if it is good, process their request and respond with a new access token. if the access token has expired, make them go to the endpoint to get a new access token.

For the access token, you can set it up to expire after a certain time. you would do something like:

var timeDiff = DateTime.UtcNow - AccessToken.DateTime;
if (timeDiff.Minutes > 3)  // expired
else                       // still good

This is sort of something you'd do, although getting the access token takes some more code than that.

EDIT

javascript has an interval instruction that can loop on a separate thread and delays for a timeperiod. you could have it run checking the access token seeing if it is going to expire. when there is like a minute until (or whatever) it expires, prompt the user. it is still best practive to have something server side as well

czifro
  • 784
  • 7
  • 24
  • Thank you for the answer, @czifro. However, you are thinking of a scenario different from what I am talking about. You assume that the requests keep coming in and the logic you suggest detects "late arrivals". What I am after is the scenario when the remote client dies or loses connectivity. There are no queries coming in, and I cannot trigger the time checking logic off of it. This is why a timer sounds like a better option that generates and event that I can trigger on. I am not sure if this is a best practice in ASP.NET world, though. – evolvah Jul 27 '14 at 18:41
  • this doesn't check late arrivals, the way this acts is like most sites that require some sort of login. if you take a banking site, they send you a token when you login in. so long as you are active the token is good. after about 10 min of inactivity, the token is no longer good. the server won't necessarily assume connection was lost, rather invalidate the token the next time it is used forcing the user to get a new token for the new session. – czifro Jul 27 '14 at 19:01