How to Display Online Users in Real-Time
Published: 2024-02-13 08:48:36

Get ready to build a real-time online users widget using JavaScript and HTML, no prior backend experience necessary, perfect for beginners! By the end of this tutorial, you'll be able to add a WordPress-like active users sidebar to your website. This widget can significantly optimize the user experience by showing the number of active users on your website.

PubNub offers the perfect solution for broadcasting updates to any number of concurrent subscribers. With APIs such as [Real-Time Messaging](https://pubnub.com/products/pubnub-platform/) and [Presence](https://www.pubnub.com/products/presence/), you can efficiently maintain the state for a large, interactive audience.

In this tutorial, we will configure an occupancy counter using the [Presence API](https://pubnub.com/products/presence/), our online/offline detection module designed for subscribers. It will display online users in real-time as they join and leave using a widget, just like the one on a Wordpress site, which you can easily drag and drop into your web pages. This is an excellent tool for website owners who aim to enhance their site's user online status visibility. And guess what? This can all be achieved in just 15 lines of JavaScript.

![](https://www.pubnub.com/cdn/3prze68gbwl1/asset-17suaysk1qa1j1r/9fef7f30e2b95c76ff522279395c8a80/reddit.png)

Ever noticed how Reddit, a popular ecommerce platform, displays a widget in its UI that shows how many users are currently browsing a particular subreddit? Let's create something similar! Doing so can help increase your pageviews and conversions.

How to Create an App to Display Online Users
--------------------------------------------

To get started, sign up for a PubNub account and create a project in the [Admin Dashboard](https://dashboard.pubnub.com/login).

This widget utilizes the [Presence API](https://pubnub.com/docs/basics/check-user-presence), an event emitter for **join**, **leave**, **timeout** (and more) events of users that are connected to PubNub using your pub/sub keys. The Presence API also offers JSON data about the IP address of the online users. Enable the presence feature for your key and set the number of seconds between Presence updates under the settings. Don't forget to **click the save button** at the bottom of the page once you have configured everything, from frontend to backend settings.

![Presence settings interface with ON toggle, including options for Announce Max, Interval, Presence Deltas, and Generate Leave on TCP FIN or RST.](https://www.pubnub.com/cdn/3prze68gbwl1/2Svzw6ev7yrHRGW9hheONu/cc82babc7134c08e216b3e086e4eafd0/Presence_with_both_deltas_and_TCP_FIN_or_RST.png)

The Presence API has undergone some improvements over time, now allowing you to retrieve occupancy (number of active users) on multiple channels simultaneously using the multi-channel feature. All these notifications come with real-time metrics leading to much better performance and user experience. The API, now integrated with WebSockets, also offers improved performance when dealing with a high volume of connection status changes.

The following JavaScript snippet will help you set up the front-end for your widget. This code includes the latest version of the PubNub JavaScript SDK and some CSS to ensure that the widget stays fixed in the bottom left corner of a webpage, even when the user scrolls down.

Now, copy this code and customize it according to your needs. If your website uses a reactive framework like Angular, React, or Vue, I recommend using data binding to keep the number of users in sync with the data store.

```js
<!DOCTYPE html>
<html>
<head>
  <title>Currently Active Demo</title>
  <style type="text/css">
    .currently-active {
      position: fixed;
      margin: 10px;
      padding: 5px;
      bottom: 0;
      left: 0;
      border: solid 1px #AFAFAF;
      border-radius: 6px;
      font-family: "Arial";
    }
  </style>
</head>
<body>
  <div class="currently-active">
    <span>Currently Active: </span><span id="active"></span>
    <span id="ip"></span>
  </div>
</body>
<script src="https://cdn.pubnub.com/sdk/javascript/pubnub.4.41.0.js"></script>
<script type="text/javascript">
var active = document.getElementById('active');
function setCurrentlyActiveUsers(numberOfUsers) {
  active.innerText = numberOfUsers.toString();
}
setCurrentlyActiveUsers(1);
var pubnub = new PubNub({
    publishKey : '__YOUR_PUB_KEY__',
    subscribeKey : '__YOUR_SUB_KEY__',
    heartbeatInterval: 30
});
pubnub.addListener({
  presence: function(presenceEvent) {
    setCurrentlyActiveUsers(presenceEvent.occupancy);
  }
});
pubnub.subscribe({ 
  channels: ['myWebPage1'],
  withPresence: true
});
</script>
</html>
```

Related Topics
--------------

-   [What is User Presence?](https://www.pubnub.com/guides/the-importance-of-user-presence-in-real-time-technology/)
    
-   [Advent of Real-Time Apps and How User Presence Fits In](https://www.pubnub.com/blog/real-time-apps-and-user-presence/)
    
-   [Enhancing User Experience with Real-Time Presence](https://www.pubnub.com/blog/enhancing-user-experience-with-real-time-presence/)
    

Following our WordPress tutorials, you will see how easy it is to add PubNub's real-time features to your WordPress website. For instance, you can enable real-time tracking of online WooCommerce customers in no time!

How can PubNub Help You?
------------------------

Our platform assists developers in building, delivering, and managing real-time interactivity for web apps, mobile apps, and IoT devices. It's like a real-time plugin for your projects!

Our platform, built around the industry's largest and most scalable real-time edge messaging network, supports over a billion monthly active users and offers 99.999% reliability across 17 points-of-presence worldwide. This means you'll never have to worry about outages, concurrency limits, or latency issues caused by traffic spikes. To integrate PubNub with your WordPress site, you can follow the instructions in our PHP and AJAX tutorials.

Experience PubNub
-----------------

To understand the essential concepts behind every PubNub-powered app in less than 5 minutes, check out our updated [Live Tour](https://www.pubnub.com/tour/introduction/). This can also help you understand how to optimize your website’s SEO by using PubNub.

### Get Setup

Sign up for a [PubNub account](https://admin.pubnub.com/signup/) and gain immediate access to PubNub keys for free. This will take you a step closer to creating your own real-time applications.

### Get Started

The [PubNub docs](https://www.pubnub.com/docs) and our updated list of [SDKs](https://www.pubnub.com/docs/sdks/) will help you get up and running, regardless of your use case. We have several integrations and templates ready to be used with PubNub for a seamless development experience.

How can PubNub help you?
========================

This article was originally published on [PubNub.com](https://www.pubnub.com/blog/how-to-display-online-users-in-real-time/)

Our platform helps developers build, deliver, and manage real-time interactivity for web apps, mobile apps, and IoT devices.

The foundation of our platform is the industry's largest and most scalable real-time edge messaging network. With over 15 points-of-presence worldwide supporting 800 million monthly active users, and 99.999% reliability, you'll never have to worry about outages, concurrency limits, or any latency issues caused by traffic spikes.

Experience PubNub
-----------------

Check out [Live Tour](https://www.pubnub.com/tour/introduction/) to understand the essential concepts behind every PubNub-powered app in less than 5 minutes

Get Setup
---------

Sign up for a [PubNub account](https://admin.pubnub.com/signup/) for immediate access to PubNub keys for free

Get Started
-----------

The [PubNub docs](https://www.pubnub.com/docs) will get you up and running, regardless of your use case or [SDK](https://www.pubnub.com/docs)