29
What You Can Do With the Heartbeat API By Tabby Chapman @tabbymarie http://www.tabbychapman.com 1

What you can do with WordPress Heartbeat API

Embed Size (px)

Citation preview

Page 1: What you can do with WordPress Heartbeat API

What You Can Do With the Heartbeat API

By Tabby Chapman

@tabbymarie

http://www.tabbychapman.com

1

Page 2: What you can do with WordPress Heartbeat API

About Tabby Chapman@tabbymarie

https://github.com/tabbymarie

2

Page 3: What you can do with WordPress Heartbeat API

Github Public Contributions

3

Page 4: What you can do with WordPress Heartbeat API

Github Private ContributionsLike, I totally do stuff.

4

Page 5: What you can do with WordPress Heartbeat API

About Me

• Work for the University of California, Irvine

• Computer Scientist

• Developer Evangelist

• Education Junkie

5

Page 6: What you can do with WordPress Heartbeat API

About Me

• WordPress Skillz– Reaching my 10th anniversary developing

with WordPress (waiting for the party hat)– Started with 2.0.5, building a custom theme.– Now, I build highly specialized custom themes

and plugins for UCI as well as freelance clients.

6

Page 7: What you can do with WordPress Heartbeat API

About Me

• Born in Texas

• Moved 76 times (2 countries, 6 states)

• I have over 130 cousins and second cousins.

• Pink is my favorite color.

• Owls are my favorite animals.

7

Page 8: What you can do with WordPress Heartbeat API

About This Presentation

• Geared towards beginner to intermediate folks

• Assumptions:– You know what WordPress is.– You know what PHP, XML, JSON are.– You’re not afraid to look these things up later

or ask questions.

8

Page 9: What you can do with WordPress Heartbeat API

WP Heart What?

• WP Heartbeat API

• Bi-Directional Communication between browser and server… without the user ever having to trigger an event.

• Using XHR, WP Heartbeat sends ‘ticks’ every 15 seconds by default

9

Page 10: What you can do with WordPress Heartbeat API

API Information

• An API (Application Programmer Interface) is a set of methods designed to share information between systems.

• Usually uses SOAP (Simple Object Access protocol) or REST (Representational State Transfer) services.– Many cases send data via XML or JSON.

10

Page 11: What you can do with WordPress Heartbeat API

API Information

• WP Heartbeat API users:– XMLHttpRequest (XHR) to communicate from

Browser to Server– REST to send reponse from Server to

Browser

11

Page 12: What you can do with WordPress Heartbeat API

What Happens During a Beat?

• Browser sends a communication to server via XHR request. This request can contain information about the user, browser, or behavior.

• Server receives the information and may or may not do anything with it.

• Server hands a response back to Browser.

12

Page 13: What you can do with WordPress Heartbeat API

When does this happen?

• By default, every 15 seconds when logged into the admin panel.

• Can be modified with “fast,” “medium,” or “slow” values.

• Happens at this interval only when the page is targeted.

• Once untargeted or idle, the tick slows down.

• After idle for 1 hour, the tick is expired.13

Page 14: What you can do with WordPress Heartbeat API

Is there a downside?

• In the beginning there were some performance issues on some sites

• Some hosts disabled sites for too much usage

• WordPress 4.1 released fixes that expanded max heartbeat tick to 120 seconds and completely expires after an hour.

14

Page 15: What you can do with WordPress Heartbeat API

Where is it implemented?

• Initially, WP Heartbeat is implemented in the administrative panel.– Allows notification of users viewing or editing

posts simultaneously– Locks posts that are being edited– Creates Auto-save triggers for post editing.– Notifies user of session expiration (logout)

15

Page 16: What you can do with WordPress Heartbeat API

Can it be disabled?

• There are plugins available to control your Heartbeat Ticks.

16

Page 17: What you can do with WordPress Heartbeat API

What’s Under the Hood

• Filters– heartbeat_nopriv_send

– heartbeat_nopriv_receive– heartbeat_send– heartbeat_receive– heartbeat_settings

• Actions– heartbeat_nopriv_tick– heartbeat_tick

17

Page 18: What you can do with WordPress Heartbeat API

What’s Under the Hood

• heartbeat_settings filter you can modify– Interval (15 – 60 seconds)

• This overrides the default interval

– Autostart (false)• This disables autostart

18

Page 19: What you can do with WordPress Heartbeat API

How do I use it?

• Download and install https://github.com/jeffikus/pulse

• Or create your own plugin

• Use the console in your browser to view logged data

• Modify the pulse plugin to meet your needs.

19

Page 20: What you can do with WordPress Heartbeat API

Overview of Pulse

20

Page 21: What you can do with WordPress Heartbeat API

Overview of Pulse

21

Page 22: What you can do with WordPress Heartbeat API

Overview of Pulse

22

Page 23: What you can do with WordPress Heartbeat API

Overview of Pulse

23

Page 24: What you can do with WordPress Heartbeat API

Overview of Pulse

24

Page 25: What you can do with WordPress Heartbeat API

Overview of Pulse

25

Page 26: What you can do with WordPress Heartbeat API

Overview of Pulse

26

Page 27: What you can do with WordPress Heartbeat API

Demo

27

https://github.com/tabbymarie/hb-path

Page 28: What you can do with WordPress Heartbeat API

What are some uses for this?

• Alerting users of new content posted

• Letting a user know when a friend has signed on.

• Creating a means of real-time-ish private messaging

• Dynamically updating widgets/sidebars (i.e. various advertisements)

• Editors can change content mid-read.

28

Page 29: What you can do with WordPress Heartbeat API

Resources

• https://github.com/jeffikus/pulse

• https://github.com/tabbymarie/pulse

• https://github.com/tabbymarie/hb-path

• https://pippinsplugins.com/using-the-wordpress-heartbeat-api/

• http://code.tutsplus.com/tutorials/the-heartbeat-api-getting-started--wp-32446

• https://developer.wordpress.org/reference/hooks/heartbeat_settings/

29