This page is an archive of my old blog. Please visit for my current blog.
This site is no longer being maintained and commenting is disabled.

AIR Tip 1 – Monitoring Your Internet Connection

AIR is meant to facilitate applications that work when online and offline. For example, if you developed an AIR application to manage your blog, you would want to be able to write blog posts whether you were online or offline. To accomplish this, the application would do one of two actions depending on whether it had an internet connection. If it were online, it would take your post and upload it. If it weren't online, it would store it (either in a file or in a local SQLite database).

VERSION: This tutorial is current for AIR Beta 3.

Today, you are going to build a very simple AIR application. If will be a window that will have three main items: a search box, a submit button, and an image that indicates if you are connected to the Internet. If a user types something into the text input and hits the search button, it will open up your browser and search for that term on Google. However, if you are not connected to the Internet, the "search" button will be disabled.

AIR has two specific functions for monitoring your internet connection, URLMonitor [ Flex | Javascript ] and SocketMonitor [ Flex | Javascript ]. These classes both implement the ServiceMonitor Class [ Flex | Javascript ]. To monitor your connection you just follow the steps below:

  1. Create a URLRequest with the URL that you want to monitor. You can set its mode to "HEAD" to avoid getting the entire page every time.
  2. Create a new URLMonitor and assign it the URL that you want it to monitor.
  3. Add a new Event Listener for the URLMonitor that listens for the StatusEvent.STATUS event and create the function to act on that event.
  4. Set how often you want to the URLMonitor to run and start it.

If any of this seems confusing, don't worry, you will look at each item in the code below. Also, the full source code will be available for both examples.

Coding the Example

For the application, you are going to create a function that gets run when the application starts. This will be where you will create your URLRequest and URLMonitor objects. For Flex, this will be a function that responds to the CreationComplete event, for the HTML/Javascript example, this will be a function that responds to the "onload" event of the body tag.


  1. import;
  2. import;
  3. import;
  5. // DEFINE The Variable that will hold the URLMonitor
  6. private var monitor:URLMonitor;
  8. private function init():void {
  10. // URLRequest that the Monitor Will Check
  11. var url:URLRequest = new URLRequest("");
  12. // Checks Only the Headers - Not the Full Page
  13. url.method = "HEAD";
  15. // Create the URL Monitor and Pass it the URLRequest
  16. monitor = new URLMonitor(url);
  17. // Set the Interval (in ms) - 3000 = 3 Seconds
  18. monitor.pollInterval = 3000;
  19. // Create the Event Listener that Will Be Fired When Connection Changes
  20. monitor.addEventListener(StatusEvent.STATUS,on_connection);
  21. // Start the URLMonitor
  22. monitor.start();
  24. }


  1. var monitor;
  3. function onLoad() {
  5. // URLRequest that the Monitor Will Check
  6. var request = new air.URLRequest( "" );
  7. // Checks Only the Headers - Not the Full Page
  8. request.method = "HEAD";
  10. // Create the URL Monitor and Pass it the URLRequest
  11. monitor = new air.URLMonitor( request );
  12. // Create the Event Listener that Will Be Fired When Connection Changes
  13. monitor.addEventListener( air.StatusEvent.STATUS, doStatus );
  14. // Start the URLMonitor
  15. monitor.start();
  17. }

Hopefully you can see that there is not a great deal of difference between the two. Calling an AIR function within Javascript is just as easy as it is in Flex.

NOTE: If you want this javascript to function properly, be sure to include the AIRAliases.js file and the servicemonitor.swf file in your application. If you do not, this code will not function properly. Your code can function without the AIRAliases.js - the method names are just longer. However, you cannot use the URLMonitor or SocketMonitor without the servicemonitor.swf file.

Flex Application
Source Code

HTML / Javascript Application
Source Code

16 Responses to “AIR Tip 1 – Monitoring Your Internet Connection”

  1. Matt Graf says:

    I am trying to run this app and I am getting this error
    SyntaxError: Parse error

    app-resource:/servicemonitor.swf : 1

    TypeError: Value (result of expression is not a constructor. Cannot be used with new.

    onLoad at app-resource:/tip.html : 21

    onLoad at app-resource:/tip.html : 21

    onload at app-resource:/tip.html : 72

  2. David Tucker says:

    I haven’t updated this tutorial for AIR Bet 2 yet. I am not sure if this is the cause of that issue – but I plan to update it within the next day or two.

  3. Ruben says:

    I don’t know if you are aware of it, but there’s an huge amount of spam links in your article source, hidden in a display: none bold tag….

  4. David Tucker says:

    @Ruben – Thanks! I am still recovering from a minor hack a while back.

  5. [...] AIR技巧 AIR Tip 1: Monitoring a Network Connection (AIR Beta 3) AIR Tip 2: Going Fullscreen (AIR Beta 3) AIR Tip 3: What Version is My Application [...]

  6. [...] AIR Tip 1: Monitoring a Network Connection (AIR 1.0) [...]

  7. Adrian Parr says:

    Also worth mentioning is that if you want to do this using Flash instead of Flex or Javascript then you will need to drag a copy of the ‘ServiceMonitorShim’ component (found in the Components panel under AIR ServiceMonitor) in to your Library.

    If you don’t do this you will see some/all of the following compiler messages …
    1046: Type was not found or was not a compile-time constant: URLMonitor.
    1180: Call to a possibly undefined method URLMonitor.
    1046: Type was not found or was not a compile-time constant: URLMonitor.
    1120: Access of undefined property URLMonitor.
    1172: Definition could not be found.

    This can easily catch people out (including myself).

    Hope this helps someone.



  8. In case anyone ever gets the first commenter’s issues, you must make sure to include the ServiceMonitor library (either in Flex, Flash, or HTML … which all have differing methods of inclusion), and also make sure it’s case-sensitive. I had a lowercase filename (in Windows) and it would keep throwing the error until I fixed the case sensitivity of the file in my application.

  9. Bond guy says:


    came across this example.
    i am a noob to flex / AIR .

    Is it possible to implement this example inside flex app(not AIR) , to check the connection ? If yes, do urlMonitor and SocketMonitor have a counterpart in flex ?
    HttpRequest and other URLLoader functions are not compatible with various browsers and hence not reliable .

    The goal is to simulate connection monitoring inside a flex app (swf)

  10. joan.mmk says:

    Thanks Adrian, you really help me.

  11. [...] of methods to detect network connectivity, all of which can be read about here, here, here and here. What I found is that these seem to work fine when there is an actual change in your network [...]

  12. Nino says:

    Hi ,
    I cant not get this code to work for adobe air 2 beta 2
    can somebody please help ?

    i m getting the error : TypeError: Result of expression ‘air.URLMonitor’ [] is not a constructor.

  13. Nino says:

    David any help please ? :(

  14. phish says:


    it works but it’s far from real time. It takes very long to detect that a network connection is dead but it’s very quick to detec it’s up again. I thougth about idleTimeout but it does not work. Any idea ?

  15. dizda says:

    Thank you for your precious advice David, I forget to use “PollInterval”, but its ok now thanks to you :-)