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.For example, the message publish event is triggered whenever a PubNub message is published or the user updated event is triggered when a User's metadata changes. For a full list of events, see the event documentation.
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 extensive, including webhooks, Amazon SQS, Amazon Kinesis, S3, Apache Kafka, and more. 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:
- Listen for the ‘before message publish’ event, intercept the message and moderate the message data to detect abusive language or profanity.
- Listen for the ‘before file publish’ event and re-route any images to a moderation channel whose contents are potentially harmful.
- Listen for the ‘after message publish’ event and forward messages to an external system to perform sentiment analysis on the chat, which is used frequently by customer support.
- Listen for the ‘before message publish’ event and translate the language within the message or message parameters to perform on-the-fly translation.
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?
Technically speaking, everything Events & Actions can do, you could also accomplish with Functions, but the two were designed for different purposes: Functions are intended for manipulating messages as they pass through PubNub, whereas E&A has been designed specifically for data export.
- If you need to send real-time data moving through PubNub to a third-party system, Events & Actions should be your first choice.
- If you need to consolidate data from many different devices, and queue it before it can be processed, you should use Events & Actions.
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:
- Errors & authentication/permission issues generated by users and devices need to be saved to Investigate later problems
- Auction bids need to be stored to maintain a record for legal purposes.
- IoT device sensor data from many different devices needs to be stored centrally in a long-term archive.
Data analysis:
Using an external analytics platform, you can transfer data with little latency and perform analytics to understand your solution better. Some examples:
- After-the-fact sentiment analysis and metrics on chat messages to understand your customer experience
- Using historic chat messages to train an AI bot
- Reviewing geolocation coordinates to ensure routing algorithms are operating correctly
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:
- After Publish or Fire
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, strongly 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?
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.
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 30 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.