Manipulate, Augment, Filter Data Streams using Functions

5 min read Chandler Mayo on Aug 13, 2019
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.

A guide to shaping data and messages in any PubNub app.

PubNub Functions makes it easy to route, filter, transform, augment, or aggregate real-time messages that are transmitted over the PubNub Data Stream Network. With PubNub Functions, you can take precise control over your messages without routing messages to your own server or making your application more complex.

In this tutorial, we’ll be using PubNub Functions to prevent multiple push notifications being triggered from a Smart Doorbell app built in React Native and a PubNub-powered IoT smart button. You can use this tutorial as a guide to shape data and messages in any PubNub app.

Mobile Push Notifications

In our Smart Doorbell app, a user can press a smart button to trigger a remote push notification, but nothing stops a user from pressing the button repeatedly. Using PubNub Functions we will implement logic within the PubNub Network to remove the push notification keys from the message for messages sent less than 30 seconds from the last push notification. This kind of functionality is commonly known as debouncing. Debounces are often used in programming to ensure that tasks do not fire so often. Since we’re using PubNub Functions to manipulate the data, we won’t need to make any changes to our PubNub powered IoT smart button or the Smart Doorbell app.

What are PubNub Functions?

PubNub Functions is a serverless environment for executing functions on the edge, transforming, enriching, and filtering messages as they route through the PubNub network. PubNub Functions are JavaScript event handlers that can be executed on in-transit PubNub messages or in the request/response style of a RESTful API over HTTPS.

If you’ve used Node.js and Express, the “On Request” event handler development will be second nature.

Here are some of the things you could use PubNub Functions to do:Functions Logo

  • Secure API Calls – Trigger an alert when a secure message meets certain criteria – email, text message, tweet, mobile push, etc.
  • Decrease Time to Market: Focus on building your product and not managing the infrastructure to support it.
  • Reduce LatencyYour users are global. Your product should be global. PubNub operates points of presence across the globe.
  • Translation – Send a message in English and receive it in Spanish with a 3rd part translation API.
  • Network Logic – Reliably count up millions of votes during real-time audience interaction.
  • Serve Data – Build a globally replicated, low latency, production REST API in 5 minutes.
  • Reduced Cost: Pay for usage and not idle servers.

Getting Started

You’ll need a free PubNub account and a React Native app.

Your app does not have to be the Smart Doorbell app we’re using in this tutorial. You can use your own PubNub application or you can build the Smart Doorbell app.

Here’s what you need for the Smart Doorbell app:

Smart Doorbell Events

IoT Smart Button

IoT Smart Button

You’ll need to build your own PubNub powered IoT smart button to use with the Smart Doorbell app. I’ve put together a guide to build your own IoT smart button.

You only need two hardware components:

Build your button by following the guide.

Smart Button React Native App

Next, you’ll need to complete the tutorial for building a Smart Doorbell app in React Native that tracks the history of the smart button press events. The app works on both iOS and Android.

Build the Smart Doorbell app by following the tutorial.

Push Notifications

Last, you’ll need to enable push notifications in the app by completing the tutorial for Instant Push Notifications for iOS and Android from a React Native App.

Creating a Function to Alter Messages

Go to your PubNub Admin Dashboard and select your app. Then click on the keyset that was created for that app.

Next, click “Functions” in the sidebar and then create a new module named “PushFilter.” Select the module you just created.

Create a Function with the name “Push-Filter.”

Set the event type to “Before Publish or Fire”, and set the channel name to “smart_buttons”.

Delete any code in the editor and replace it with a function to send an altered message if messages are sent less than 30 seconds apart:

const store = require('kvstore');

export default (request) => {
    // Limit push notifications.
    const duration = 30;  

    // Get the time and compare to the last time a notification was sent. 
    const currentTime = Math.round( / 1000);

    return store.get('anti_spam').then((value) => {
        // Get the last time a message was sent with push notifications.
        const lastMessageTime = value.last_message_time;
        // Edit message if duplicate.
        if ((currentTime - lastMessageTime) < duration) {
            // Modify the message to remove push keys.
            console.log("Duplicate push notification removed.");
            request.message = { "event": { "button": "pressed" }};
        } else {
            // Allow full message and record time.
            store.set('anti_spam', {
                last_message_time: currentTime

        return request.ok();
    .catch((e) => {
        return request.abort(); 

Click “Save” and then “Restart module.”

Test Message Debounce with IoT Smart Button

Press your PubNub powered IoT smart button and you’ll see in the console that an unaltered message is allowed only once every 30 seconds. No more push notification spam!

Push Notification Spam Filter

Test Message Debounce with Debug Console

You can also test by sending messages to the “smart_buttons” channel from the PubNub Debug Console. Go to your PubNub Admin Dashboard, select the keys you created for this project, and create a client in the Debug Console. Set the “Default Channel” to “smart_buttons” and leave the other fields blank. Click “ADD CLIENT”.

Send a test message to the channel and you’ll see that an unaltered message is allowed only once every 30 seconds.

  "event": {
    "button": "pressed"
  "pn_apns": {
    "aps": {
      "alert": "Someone is at the door."
  "pn_gcm": {
    "notification": {
      "body": "Someone is at the door."

What’s next?

Have suggestions or questions about the content of this post? Reach out at

More from PubNub

Top Trends to Engage Your App Users
InsightsSep 27, 20224 min read

Top Trends to Engage Your App Users

Take a look at the top trends that are the most effective in attracting customers and reducing churn, and how you can incorporate...

PubNub Staff

PubNub Staff

Comparing Game Engines: Unity vs Unreal vs the Rest
Real-Time Gaming BlogSep 21, 20225 min read

Comparing Game Engines: Unity vs Unreal vs the Rest

Comparing the major game engines: Unity vs Unreal Engine vs Corona SDK vs GameMaker Studio, including the benefits and cons of...

PubNub Staff

PubNub Staff

Python Socket Programming: Client, Server and Peer Libraries
BuildSep 21, 20226 min read

Python Socket Programming: Client, Server and Peer Libraries

Sockets (aka socket programming) enable programs to send and receive data, bi-directionally, at any given moment. This tutorial...

PubNub Staff

PubNub Staff