Pinging a server is used to determine whether it is online or not. The idea is to send an echo message to the server (called ping) and the server is expected to reply back with a similar message (called pong). Ping messages are sent and received by using ICMP (Internet Control Messaging Protocol). The lower the ping time, the stronger is the connection between the host and the server.
Approach: One obvious approach is to use the command prompt for sending ping messages to the server. Please refer to this post for details. In this article, we’ll be using Ajax to send a request to the required server and then examining the status code received to find whether the server is running or not. The idea is that a server is definitely up and running if it returns a status code 200. Other status codes like 400 etc. point toward a possible server outage.
Below is the step by step implementation:
Step 1: Create a file named ‘index.html‘ file to design the basic web page. The function “pingURL” is invoked when a user clicks on the button on the web page.
Step 2: Create the ‘index.js‘ file to make a request to the server. The “pingURL” function defined the required configuration for the Ajax request in the ‘settings’ variable.
- Open the web page in your web browser.
- Press ‘Ctrl+Shift+I‘ to navigate to Browser Developer Tools.
- Enter the URL you wish to ping in the form input and click the ‘Submit’ button.