Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Notification polling on the client #397

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from

Conversation

filipac
Copy link

@filipac filipac commented Aug 9, 2023

What?

This PR adds notification polling, currently based on the heartbeat API from WordPress.

Why?

This attempts to solve the following issue: #306

How?

This PR adds a new Poller class that abstracts how/when/how often the notifications are polled.
It makes sense to use the existing Heartbeat API from WordPress since it is reliable. The Heartbeat API is a simple server polling API built into WordPress, allowing near-real-time frontend updates.
Heartbeat API Docs

The Poller class hooks into the heartbeat.tick event, which is triggered on a successful heartbeat (in case the user session expired or there is another error, we also skip useless polls to the API).

There is a parameter that is passed to the Poller class named shouldSkipFirstBeat which solves the following edge-case: we load all notifications on page load, but also on some pages, the heartbeat API runs very early on the page load (eg: on post edit/create screen, on post list).
To avoid double, repeated polls to fetch notifications, the first Poller.SKIP_FIRST_INTERVAL milliseconds, any heartbeat tick events are ignored.

The Poller instance is added to the notifications (window.wp.notifications) object, so you can easily do:

  • window.wp.notifications.poller.stop() - Stop polling
  • window.wp.notifications.poller.start() - Start/resume polling

Testing Instructions

  1. Visit any page on wp-admin
  2. Wait for a heartbeat tick event
  3. Notice the polling request.
  4. Visit the posts page. Check that only one call is made on page load.

I am open to suggestions and ideas, also if you want to use a setInterval/setTimeout instead of the Heartbeats API, let pe know. I think it is appropriate though.

Closes #306

@Sephsekla Sephsekla self-assigned this Oct 4, 2023
@Sephsekla
Copy link
Collaborator

@filipac my apologies for leaving this unreviewed for so long! Unfortunately things have been on hold a little bit due to team availability and illness, but I'm eager to get things rolling again so will give this a look shortly.

Copy link
Collaborator

@Sephsekla Sephsekla left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fantastic work, I can only apologise for how it's taken to look this over properly.

We may well need to iterate on this in future (like a lot of the plugin), but for now I think it's a great approach.

@EdithAllison
Copy link
Collaborator

EdithAllison commented Nov 15, 2023

Heartbeat may be turned off as it's got a reputation for slowing sites down, and if you google WP performance hacks, disabling or limiting Heartbeat often comes up.

Plugins that disable or limit Heartbeat (not a comprehensive list):
https://perfmatters.io/
https://wordpress.org/plugins/heartbeat-control/
https://wordpress.org/plugins/sg-cachepress/
https://wordpress.org/plugins/litespeed-cache/
or search on WP plugin directory: https://wordpress.org/plugins/search/heartbeat/

Articles:
https://perfmatters.io/docs/disable-wordpress-heartbeat-api/
https://www.wpbeginner.com/plugins/how-to-limit-heartbeat-api-in-wordpress/
https://www.hostinger.com/tutorials/wordpress-heartbeat

If we rely on Heartbeat we may first need to check it's enabled, and runs often enough. Maybe add a notification / warning if it's turned off.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement notification polling on the client
3 participants