Beginner guide about heartbeat API

Beginner guide about heartbeat API

WordPress heartbeat API is introduces in WordPress 3.6. WordPress heartbeat API allows your browser to communicate with the server using AJAX calls when you’re logged into the WordPress admin panel. This functionality allows WordPress to handle things like showing other authors that a post is being edited by another user, plugins can use these script executions and show you notifications in real time, etc.

heartbeat_control_plugin

How the heartbeat API Works?

If you monitor server requests while in the post editor, you’ll see

<<<POST /wp-admin/admin-ajax.php “http://example.com/wp-admin/index.php>>>

appear every 15 seconds. This is the pulse generated by the heartbeat API.

The pulse is two way communication protocol that makes sure you have a persistent connection to the web server and provides a way for developers to trigger events. For example, if the pulse takes 30 seconds or longer to process, WordPress displays a connection error and attempts to reconnect to the server.

Real time examples about heartbeat API work

If there are many routine tasks that you want to make them automatically, you can use heartbeat API to execute them effectively. These routine tasks can provide real-time information and sync the data on the server and dashboard instantaneously.

  • Autosave and revisions in the post editor.
  • Notifications on WordPress admin dashboard.
  • Post-locking information when another editor is working on a post.
  • Real-time data displayed on the dashboard by plugins like Ecommerce.

The problem

Heartbeat API may generated many many pulses as the tasks need and these actions may be harmless and very helpful to achieve many tasks.

The problem begins when each user who has access to the backend of WordPress generates a pulse. WordPress heartbeat API uses AJAX calls to the server. This may increase the overall pulses as so the requests on the server and drain the resources.

If a web hosting provider has strict limits on the number of processes or resources used, you could easily go over the limit. Or if the web hosting was shared, it may lead to your account suspension.

How to monitor the heartbeat API?

There is no direct method to help you monitor the heartbeat API actions but it’s according to the web hosting server provider where it may provide some additional tools for that.

If you see <</wp-admin/admin-ajax.php>> on the top of the list, that’s it. The WordPress Heartbeat API has drained the CPU.

After that you may be able to control it or disable it once and for all.

How to disable WordPress heartbeat API?

In most cases you can completely disable Heartbeat if you’re the only person working at any given time in your site and you know that you don’t have any important functionality that heavily relies on it to work properly.

To disable it, edit the “functions.php” file of your theme and paste these lines right after the opening <?php tag:

 

add_action( ‘init’, ‘stop_heartbeat’, 1 );

function stop_heartbeat() {

wp_deregister_script(‘heartbeat’);

}

 

This should completely disable this functionality

How to control WordPress heartbeat API?

There is a plugin which called “Heartbeat control”. After the installation and activation, you can navigate to “Heartbeat Control” option under “Settings” menu.

heartbeat_options

From the Heartbeat Behavior drop-down choose Modify Heartbeat. Then select all Locations and from the Frequency slider choose 60 or above. Finally, save the changes.

There are many other options to disable the heartbeat without modifying the code.

Important Note

If you decided to disable the heartbeat, you need to know the consequences for this action.

  • Auto save and revisions will not work. This means you need to press the “Save Draft” button manually to save your content.
  • In case if the connectivity is lost and you unknowingly pressed “Publish / Update / Save Draft” button then the content you made online will be lost.
  • You may not be able to see real-time statistics and information if any  of your installed plugin uses heartbeat API to update the content from server.

You need to control heartbeat API not only to save resources but to increase the response of the website and the server.

Leave a Reply

Your email address will not be published. Required fields are marked *