Deephaven performs exceptionally well with real-time data. Throughout this blog series, we've demonstrated how to ingest RSS data feeds as an example of frequently changing data and how Deephaven empowers you to isolate what you care about from that sea of information. One option is the ability to create a listener on a table. A listener is simply a function that executes anytime a table is updated. In this blog, we will show how to add listeners to extend the RSS podcast exploration application to send notifications when certain podcasts are found.
Filtered data
The code in this blog will extend the RSS podcast aggregator sample app. If you're unfamiliar with that code, consider reading our previous RSS podcast exploration blog post.
To start, let's run a few filter queries on our RSS podcast feeds data. This allows us to work with subsets of our real-time data.
This results in two filtered tables, one containing entries that have tech keywords and the other containing entries that were published in the last 24 hours.
Listener
We can now add listeners to these filtered tables, allowing us to execute functions whenever the tables are updated. Let's start with simple listeners that create a print statement when a table is updated.
The listener functions take a single argument that represents the update object.
Let's extend this example by printing the title of the added entry. To do this, we will need to access the update object's iterator methods and the associated table.
And now we have a listener that logs all the new entries in our tables. Listeners can execute any Python function, so you can use them to perform tasks such as Slack notifications, send emails, send text messages, and create HTTP requests.
Parameterizing the listener
You may notice in the above example that we have two functions that are almost identical. This isn't a problem with a small number of listeners and tables, but if we were to write several more similar listeners, we'd have a lot of duplicated code. How can we work around this to parameterize our listener?
Using the above example, ideally we'd want to parameterize the table and the print statement to end up with a listener function that takes those arguments along with the update object. Unfortunately, we can't change the method signature of the listener functions. However, we can create a function that takes our first two parameters (the table and print statement) and returns a function that takes the update object. If you're familiar with functional programming, this is very similar to currying.
Let's rewrite our print_entry listener function accordingly.
The print_entry_builder function takes our two parameters (table and table_name) and defines the print_entry function that takes the update object. Because of Python's variable scoping, print_entry will now reference the parameter table and table_name objects. This allows print_entry_builder to return different print_entry functions based on the table and table_name parameters.
We've now successfully reused our listener function across multiple tables.
Slack notifications
As stated above, listeners can pretty much be any function. The RSS podcast aggregator sample app shows an example of how you could create a Slack listener.
Simply plug in your SLACK_API_TOKEN and SLACK_CHANNEL_ID environmental variables to use this listener. You'll get a Slack message anytime your chosen keywords show up in the RSS table.
Reach out
If you use this project and blog post as a baseline for working with Deephaven, we'd love to hear about it. Let us know what you come up with in our Github Discussions, or our Slack community.
