Events & Actions or Functions: Picking the right tool

Default-Social-Image.png

You may already be familiar with Functions since it is used by many of our existing customers to augment their PubNub solutions with custom server logic using JavaScript.  Events & Actions is the newer but complementary feature that provides a zero-code way to extract PubNub real-time data into third-party systems for later storage or analysis.  

This article will explain the differences between Events & Actions and Functions and which use cases suit each.

What are Events & Actions?

Events & Actions enables you to filter the real-time “events,” queue those events, and deliver them to a growing list of “actions.”

So, what is an “event”?

Events are generated by users and devices interacting with the PubNub platform, and anything that happens during that interaction could generate an event.

The message publish event is triggered whenever a PubNub message is published and expect to see additional events added in the future.

So, what is an “action”?

An action is performed when an event is triggered and will send the event data to a remote endpoint.  The catalog of actions is growing but, at launch, includes webhooks, Amazon SQS and Amazon Kinesis.  All actions will send your data outside of PubNub to a third-party system that you control for additional processing.

That feels like a lot of data.

It can be, but Events & Actions features an extensive filter mechanism. You can choose between simple filters, which are easy to configure, or more advanced filtering based on JSONPath to cover all use cases.  For more information on E&A filters, including how to filter on your JSON payload, please refer to our documentation's “configure Events & Actions” section. 

What are Functions?

Functions allow you to capture various events on the PubNub platform and write serverless Node.JS code to handle those events.  Functions are very flexible and enable you to transform, re-route, augment, filter, and even aggregate data.

Some oft-cited use cases for Functions show their more common uses:

There is a comprehensive list of events that can trigger synchronous and asynchronous functions, which, combined with the flexibility of Node.JS and the JavaScript programming language, you can accomplish a wide variety of use cases.  Custom integrations also exist to add functionality to your PubNub solution quickly, from voice API calls to SMS to chatbots and everything in between.

When should you use Events & Actions?

Everything Events & Actions can do, you could also accomplish with Functions, so why choose E&A?

If you need to send real-time data moving through PubNub to a third-party system, Events & Actions should be your first choice. Specifically for this use case, E&A is more scalable and fault-tolerant.

Events & Actions is also a zero-code solution, so if your developers are busy doing other things (like adding real-time capabilities to your app using PubNub SDKs), then E&A is a good choice.

Some everyday use cases for which Events & Actions is ideal:

Storage:

Many customer solutions sending real-time data over PubNub need to archive that data externally in data centers.  Some examples:

Real-time data processing:

Sometimes data that originates from PubNub needs to be routed to an external system for additional processing.  Some examples:

Data analysis:

Using an external analytics platform, you can transfer data with little latency and perform analytics to understand your solution better.  Some examples:

Should I change my existing app to use Events & Actions? 

Maybe.

Ask yourself a couple of questions:

Question 1: Am I using a Function triggered by the ‘after publish’ event?  Or am I subscribing to PubNub messages somewhere in my backend infrastructure?

If you have a Function that responds to the following events:

And within that function, if you transfer that data outside of PubNub, you should consider transitioning this logic to Events & Actions.  In this instance, E&A will be more scalable, fault-tolerant, and does not require any code to be written.

Alternatively, look for instances of:

PubNub.subscribe({channels: [...]},);

Being called at your server side.  The most common reason to subscribe for PubNub messages on your server is to reroute those messages off-domain.  If this is you, consider transitioning this logic to Events & Actions, which you will find more scalable.  

Question 2: Are any of my use cases suited to Events & Actions?

If your use cases pertain to storage, processing, or real-time data analysis, as detailed in the previous section, you should seriously consider using E&A.  

If your solution transfers real-time data outside of PubNub for any reason, then E&A is likely the best way to achieve that.  

When should you use Functions?

Although everything E&A can do, you could accomplish with Functions, the reverse is not true.  Functions are more feature-rich than Events & Actions and should still be used for event-driven processing outside the specific use cases for E&A discussed in this article.  If you need to perform any event-driven processing that does not involve transferring the data to an external server, continue to use Functions.

Functions support many synchronous (blocking) events, which allow you to transform data before it is sent to clients.  If you are changing message data, including the chat moderation or language translation use cases given earlier, then you should continue to use Functions. 

Suppose you need to perform custom business logic on the PubNub data (or metadata) before it reaches your external system, like aggregating data. In that case, you should continue to use Functions. 

Since E&A only currently supports the message publish event, If you have existing functions that derive their data from different events, continue using Functions.

Also, only Functions support code. If you already use external integrations or need to run code to process the data before it is sent out externally, continue to use Functions.

In Conclusion

If you need to send real-time PubNub data to a third-party system, Events & Actions should be your first consideration.  If you need to do anything else with your real-time data, use Functions.  Whether you use E&A or Functions for your event-driven processing, PubNub gives you complete and flexible control over your data.

If you already use PubNub, go to the admin dashboard to enable Events & Actions for your account. We have how-tos for setting up E&A and filtering messages using JSONPath

If you are not using PubNub, sign up for a free trial to see how easy it is to get started. Beyond the free trial, see our pricing page or contact sales to discuss your specific needs.

PubNub powers applications that enable low latency real-time user experiences with scalability in mind, from in-app chat apps & live events to data streaming, pub/sub, mobile push notifications, and HIPAA-compliant healthcare apps. PubNub supports over 50 SDKs, including Rest API, Java & mobile app APIs for Android and iOS. Please see our tutorials and open-source samples available on GitHub to learn more.