Important note: This blog post contains information about a developer preview CURL function in Eventing. The CURL function will be changing in Couchbase Server 6.5, please see Using cURL with the Eventing Service: Update for more details.

Eventing is a new service available in Couchbase Server. The Cincinnati Reds are interested in using this feature to more quickly serve their VIP customers. A concierge is assigned to one or more VIPs. When the VIP enters the ballpark, a text message will be sent to the concierge. The concierge can then meet the VIP at their seats.

As a proof of concept, I’ve created an example that uses Functions (part of Eventing) to send a text message. I’m using a trial account of Twilio to send the messages. I don’t know if the Reds plan to use Twilio or text messages in their production system. But it’s free and easy to get started using Twilio, so that’s what I decided to use in my proof of concept. You can use any messaging/notification service you’d like (instead) as long as it has an HTTP API.

Eventing review

I’m not going to provide all the details for eventing here. This has been blogged about plenty already by my colleagues. Definitely check out their posts for more details:

But the short story is this: Couchbase Functions (part of Eventing) allow you to write JavaScript functions that respond to documents getting created/updated/deleted. Inside of these functions, you can read documents, write documents (to other buckets), execute N1QL queries, and execute a curl function to make requests to HTTP endpoints. Note: The curl construct is still in Development. This feature is intended for Development purposes only and should not be used in Production environments.

I’m going to put together pieces of the above blog posts to create this eventing proof of concept for the Reds.

Scanning Tickets

The first step is to scan a customer’s ticket at the gate. I wrote a program to simulate this. It will create a randomish “ticketscan” document in a “tickets” bucket on Couchbase.

Before scanning tickets, I needed to prepopulate the bucket with some data.

I’ve decided that customers 1 through 9 are the VIPs, and any other customer is a “regular joe”.

Fun Note: these VIPs are all actual Reds fans!

I also created 3 concierges and divided up the VIPs amongst them.

In the above example, I’m assigning customers 1, 2, and 9 to “Matt Groves”. This means that George Clooney, Josh Hutcherson, and Ryan Collins are the VIPs that concierge Matt Groves is assigned to take care of. (Replace _yourVerifiedNumber with the phone number that you’ve confirmed with Twilio).

I’m also storing Twilio credentials in a document. I did this because I’ll need the credentials inside of a Couchbase Function, and I didn’t want to hard-code them there. The credential document looks like:

I’ve created a console app that will create a new “ticketscan” document. When you run it, you can choose to create a VIP scan or a “regular Joe” scan.

A ticketscan document contains only three fields: the ID of the customer document, a timestamp, and a seat number.

You can find the full source code on Github.

Eventing function

The core of this eventing example is the function. When I created this function, I called it “notifyConcierge”; I used a “tickets_metadata” bucket (as pictured below). Most importantly, I created an alias for the “tickets” bucket and called it “src”. Within the function, this bucket is read-only, but I need it to get customer and concierge information as well as the Twilio credentials.

Creating a function in eventing

The entire function can be viewed on Github. Here is the breakdown of the function, step by step:

1 – Is the mutated document a ticketscan? If it is, proceed. If not, this function can ignore it.

2 – Is the customer who scanned this ticket a VIP? If so, get the concierge’s details and proceed. Otherwise, ignore it. Notice the inline N1QL in this part of the function. This is a feature unique to the flavor of JavaScript used in Couchbase functions.

3 – Get the details of the VIP.

4 – Get Twilio credentials.

5 – Construct a message (containing VIP’s name, seat number, and concierge’s name). data is the minimum needed to use Twilio’s API.

6 – Send a text message to the concierge using Twilio API. The curl construct is still in Development. This feature is intended for Development purposes only and should not be used in Production environments.

Eventing in action

Now, whenever a ticketscan document is created or modified, a concierge will be notified on their cell phone. Here’s an example of the entire demo, from ticket scan to SMS notification:

Eventing demonstration using SMS

In this image, I’m creating 4 ticket scans. The first is for a VIP, the next two are for regular Joes, and the last is for a VIP. Two text messages appear. In reality, these would appear on different phones, but I’m sending all the notifications to a single number for testing. Note: I’m using join.me to show my live Android screen side-by-side with the console).

If you’re running into any issues, the good news is that debugging is available for Functions in Couchbase. You can set breakpoints in the JavaScript and step through it using a tool like Chrome. For more about it, check out the Eventing announcement post.

Summary

With this function running in Couchbase’s eventing system, every new ticketscan for a VIP will trigger a text message notification. Note that the ticket scanning system doesn’t need to know anything about Twilio, as long as the data ends up in Couchbase. Also, if any other system is creating ticketscan documents, the SMS will be triggered there as well. The logic is close to the data.

This wraps up my series of Cincinnati Reds posts (for now). The other two posts in the series were:

Please leave a comment below or find me on Twitter @mgroves. Go Reds!

Author

Posted by Matthew Groves

Matthew D. Groves is a guy who loves to code. It doesn't matter if it's C#, jQuery, or PHP: he'll submit pull requests for anything. He has been coding professionally ever since he wrote a QuickBASIC point-of-sale app for his parent's pizza shop back in the 90s. He currently works as a Senior Product Marketing Manager for Couchbase. His free time is spent with his family, watching the Reds, and getting involved in the developer community. He is the author of AOP in .NET, Pro Microservices in .NET, a Pluralsight author, and a Microsoft MVP.

3 Comments

  1. Nice blog post and good timing of the blog!

  2. How to log/debug errors in the function. Basically if it doesnt work – where to debug.

    1. Jon Strabala, Principal Product Manager, Couchbase October 21, 2019 at 3:20 pm

      For logging/debugigng information refer to https://docs.couchbase.com/server/6.0/eventing/eventing-debugging-and-diagnosability.html (substitute 6.5 if your using the beta).

Leave a reply