-1

I maintain a system built with Ruby on Rails. The client has asked me to prevent the system from working when a specific user has no internet connection.

How would you do it?

I need to check if a specific computer has internet connection! For instance if my pc or your pc or my mother's pc has no internet connection!

  • Your question isn't clear. Do you want to keep your Rails app from running when it can't see the client? – the Tin Man Jan 18 '17 at 17:53
  • I don't my rails app working when it can't see the client. – Nicio Barreto Santos Jan 18 '17 at 18:24
  • @NicioBarretoSantos: if your rails stops running when there's no client, other clients won't be able to connect (the app is not working anymore) – Sergio Tulentsev Jan 18 '17 at 18:30
  • I do not know if I am being clear. I am brazilian and not good at all with english. I will try.... I need to check if a specific computer has internet connection! For instance if my pc or your pc or my mother's pc has no internet connection! – Nicio Barreto Santos Jan 18 '17 at 18:51
  • Try pinging a popular website? Basically, just try to do something over the internet and see if it succeeds or not. – takendarkk Jan 18 '17 at 18:58
  • Does your app only have one user? If so it _might_ make sense to ping and then not process if the client isn't visible or doesn't respond. If you are running a multi-user Rails app, then it doesn't make sense at all. Also, when idling, Rails won't take much CPU time at all unless you have background or cron-based code running. – the Tin Man Jan 18 '17 at 22:37
  • I don't think the server pinging the user makes sense. Instead, in the admin web page have it request a resource from the server every n seconds using a JavaScript timeout. If the server doesn't see that at its expected interval then it knows the client isn't connected. That's less costly for the server. If the client doesn't get the resource from the server have the JavaScript raise a message saying the server isn't available. Then both sides know whether the connection is up. Use a session ID or a token only known by the server and client or use HTTPS to add some security. – the Tin Man Jan 18 '17 at 22:40

3 Answers3

0

This has nothing to do with Ruby on Rails.

Is this user accessing a site that's on a local network but no outside internet access? If so you can have a before_filter or some kind of method on your homepage that tries to ping google.com or some other site outside of the LAN to check for internet.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
virtuexru
  • 858
  • 1
  • 6
  • 16
  • From the sounds of it, he actually wants the Rails application to exist in one of two states based on the connection status of one specific pre-determined user. He seems to be implementing a delivery order system for a takeout restaurant, where users can use a form to submit food orders, but only if the restaurant's computer is currently logged in to the dashboard that displays them. Which makes a lot more sense. – Ryan Plant Jan 20 '17 at 02:24
0

Use Offline.js

  1. Add offline.min.js file in your vendor/assets/javascripts
  2. Require it in your app/assets/javascripts/application.js as //= require offline.min
  3. Add the following code in the same application.js file

    $(document).ready(function() {
        Offline.check();
        Offline.on('up', function() {
            $('a.btn').removeClass('disabled');
        });
        Offline.on('down', function() {
            $('a.btn').addClass('disabled');
        });
    });
    

This is in relevance to my code where I just disable all buttons when the connection goes off.

The default behaviour of Offline.js adds a overlay with a please wait message until the internet connection comes back on.

I prefer to handle it this way as it give better user experience. Disabling buttons is a subtle way of preventing a user from working.

Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
Aniket Rao
  • 758
  • 7
  • 16
  • It's not exactly what I am searching for. Offline.js alert if the user has connection or not, and I want to get only one user. The owner of my application need to access internet to see the users requests (it is a food delivery). So if he does not has internet connection, he can't check the requests. I need to identify if he, the owner, has internet connection! – Nicio Barreto Santos Jan 18 '17 at 19:04
  • @NicioBarretoSantos: if the app is hosted somewhere in internet (say, heroku), then if manager's computer doesn't have internet connection, he won't be able to open the app. Doesn't that solve your problem? – Sergio Tulentsev Jan 18 '17 at 19:05
  • Nope! I have the delivery app and a admin. The idea is to check if someone is logged on admin, you know? – Nicio Barreto Santos Jan 18 '17 at 19:42
0

So just to clarify, your Rails application exists for users to send food delivery requests to one specific user -- the shop preparing and sending that food out, presumably -- and you want users to be unable to create new delivery requests when the shop/owner isn't currently online?

The simplest way to implement this would be to have an Admin Dashboard area where the owner can click a button to 'open the shop' and allow new orders. The worry, then, is about the shop's connection going offline, resulting in customers whose order are never received. To get around that, you can have each customer order start in an 'unconfirmed' state; when a new order is submitted, the owner is notified, and he can click a button to confirm it, letting the user know that yes, their order has been seen and is being made (and their credit card can be charged then, if you have online payment). This is probably the most reliable solution because what you really want is confirmation that a human has acknowledged the order.

If you do want to tie it directly to online status, then you can have the Admin Dashboard page regularly check that it's being viewed. You could accomplish this with a websockets connection, see Action Cable introduced in Rails 5. The Admin Dashboard page opens a websocket connection upon login; the new-connection event sets accepting_orders = true. The websocket disconnect event sets accepting_orders = false, which will happen soon after their connection is dropped (make sure to check the timeout settings). Without websockets, you could have a background HTTP request occurring on an interval; x seconds without receiving that request could cause Rails to stop accepting new orders.

Ryan Plant
  • 1,037
  • 1
  • 11
  • 18