Real-time IoT Monitoring for Devices with PubNub Presence

3 min read Michael Carroll on Aug 11, 2014
Try PubNub Today

Free up to 1MM monthly messages. No credit card required.

Subscribe to our newsletter

By submitting this form, you are agreeing to our Terms and Conditions and Privacy Policy.

How to use PubNub Presence for real-time IoT monitoring for devices to get device state, health and status.

real-time monitoring of internet of thingsWith billions of Internet of Things devices connected to the Internet, a common requirement is the need to detect the online/offline status (device state) of connected machines and devices. One way to do this is to use presence, real-time detection for IoT device state, or real-time IoT monitoring for connected devices. This blog post will take you through the steps of using presence with IoT devices and machines connected to PubNub using JavaScript and Java.

Why You Need to IoT Monitoring for Devices in Real time

Connected devices come in all shapes and sizes. We have devices that control temperature, collect sensor readings, open garage doors, and talk to other devices. We have other devices that store and protect all this data and then secure it. All in all, we need to keep tabs on all our connected devices, and know exactly when they’re online and offline.

Say you have a network of IoT devices collecting data on weather patterns around a highly sensitive area. If you’re devices go down, and that area relies on accurate, reliable reporting from your IoT devices, you need up to the second device status. In many cases, you can’t afford to be left in the dark. However, device state isn’t only essential for dramatic scenarios like this, but has become a standard for any connected IoT device for whatever reason.

Device Monitoring Using Presence

We’ll first walk you through using Presence for IoT devices with JavaScript, then cover Java. With both, you’ll first need to sign up for a PubNub account. Once you sign up, you can get your unique PubNub keys in the PubNub Developer Portal. In the developer’s portal, click to enable Presence. Feel free to play around as much as you want in our free Sandbox tier.

Check out our simulated Presence demo to get a better idea of how Presence can be used for real-time IoT monitoring for connected devices.


Step 1: The PubNub JavaScript Presence feature is an optional parameter used along with the subscribe call in JavaScript. The code sample below is basic usage:

The presence feature will output the devices that are online as identified by their UUIDs, along with their timestamp, an action that indicates join/leave/timeout and the occupancy of the channel. This information will be displayed in the console.

But what if you want to consume this information by publishing it to a screen or store it somewhere? The following code lets you do just that.

Step 2: Now we’ll bring the presence to life with JavaScript

Here, we define a custom function for presence which basically uses the different actions of a presence event that could occur, such as join, timeout and leave.

  • If a ‘join’ occurs, we append the UUID to the list of devices that are online.
  • If a ‘leave or a timeout’ occurs, we remove that UUID from the list of list of devices that are online.

You now have the online users, both in an array called ‘devices’ and also as list printed on a page.

This way, you can now be updated on the different devices joining and leaving your network in real time.

You can check out the PubNub JavaScript Presence documentation here.


Step 1: Presence and here_Now() are two features of PubNub that will show you who is online at any instant. Whether you choose to use JavaScript or the PubNub Java Presence SDK, the output for Presence is the same. You will get an output in this format:

where “uuids” contains a list of the uuids online and occupancy gives the number of online users.

I will be using the code feature to see ‘who’s there?’. All you need to provide is the channel name, and then check if there is anyone on that channel. The code sample below is basic usage.

This will output the devices that are online which is identified by the UUIDs. In order to consume this information, all you need is to modify the callback function a little. The following code shows you how:

Step 2:

This code, modifies the information received by the hereNow function, and stores and prints it in an array called ‘uuidlist’. In this manner, you can now use this information according to your requirements.

Additional PubNub Presence Resources

More from PubNub

NPP and HIPAA: Notice of Privacy Practices Definition
Healthcare CategoryJan 6, 20235 min read

NPP and HIPAA: Notice of Privacy Practices Definition

A Notice of Privacy Practices (NPP) is one of the requirements of HIPAA and helps patients understand their personal data rights.

Michael Carroll

Michael Carroll

HIPAA Violation Examples
Healthcare CategoryJan 5, 20236 min read

HIPAA Violation Examples

HIPAA violations can be financially expensive and devastating to a brand. Examine some examples of HIPAA violations, and learn...

Michael Carroll

Michael Carroll

HIPAA Technical Safeguards: How To Protect Sensitive Data
Healthcare CategoryJan 5, 20236 min read

HIPAA Technical Safeguards: How To Protect Sensitive Data

HIPAA covered entities must follow the five technical safeguards to achieve HIPAA compliance and prevent data corruption.

Michael Carroll

Michael Carroll