How to have like button update page for everyone

Viewed 35

I am writing a blog, and I want there to be a like button, but I am curious, how can I get the button to update for everybody?

<button onclick="like()">Like</button>
<span id="likecount">0</span>

and js

function like(){
  document.getElementById('likecount').innerText++;
  //Toggle code here
}
1 Answers

If you mean to update the like just like FB/Twitter/etc do, you'll need a connection that allows the server to send updates to connected clients, without the clients requesting anything. This can be done using WebSockets / Socket.io, but needs a different infrastructure than standard blogs.

An alternative would be to save the like to a database, and polling the like data every few seconds. It's less fancy, but also less complex.

Related