Important note: This blog post contains information about a developer preview CURL function in 이벤트. The CURL function will be changing in Couchbase Server 6.5, please see 이벤트 서비스와 함께 cURL 사용하기: 업데이트 에서 자세한 내용을 확인하세요.

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 하지 마십시오. 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.

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

그리고 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). 데이터 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.

요약

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 닫기 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 트위터 @mgroves. Go Reds!

작성자

게시자 매튜 그로브스

Matthew D. Groves는 코딩을 좋아하는 사람입니다. C#, jQuery, PHP 등 무엇이든 풀 리퀘스트를 제출할 정도로 코딩을 좋아합니다. 90년대에 부모님의 피자 가게를 위해 QuickBASIC POS 앱을 만든 이후로 전문적으로 코딩을 해왔습니다. 현재 Couchbase의 선임 제품 마케팅 관리자로 일하고 있습니다. 여가 시간에는 가족과 함께 축구 경기를 관람하고 개발자 커뮤니티에 참여하며 시간을 보냅니다. 그는 .NET의 AOP, .NET의 프로 마이크로서비스, Pluralsight 저자, Microsoft MVP의 저자이기도 합니다.

댓글 하나

  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, 수석 제품 관리자, Couchbase 10월 21, 2019에서 3:20 오후

      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).

댓글 남기기