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”.
1 2 3 4 5 6 7 8 9 |
_버킷.Upsert("customer::1", new { 이름 = "George Clooney" }); _버킷.Upsert("customer::2", new { 이름 = "Josh Hutcherson" }); _버킷.Upsert("customer::3", new { 이름 = "Darius Rucker" }); _버킷.Upsert("customer::4", new { 이름 = "Brooklyn Decker" }); _버킷.Upsert("customer::5", new { 이름 = "Eddie Vedder" }); _버킷.Upsert("customer::6", new { 이름 = "Nick Lachey" }); _버킷.Upsert("customer::7", new { 이름 = "Nick Goepper" }); _버킷.Upsert("customer::8", new { 이름 = "Johnny Bench" }); _버킷.Upsert("customer::9", new { 이름 = "Ryan Collins" }); |
Fun Note: these VIPs are all actual Reds fans!
I also created 3 concierges and divided up the VIPs amongst them.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
_버킷.Upsert("concierge::1", new { 이름 = "매트 그로브스", CellNumber = _yourVerifiedNumber, vips = new 목록<문자열> { "customer::1", "customer::2", "customer::9" } }); _버킷.Upsert("concierge::2", new { 이름 = "Mr. Redlegs", CellNumber = _yourVerifiedNumber, vips = new 목록<문자열> { "customer::3", "customer::4", "customer::5" } }); _버킷.Upsert("concierge::3", new { 이름 = "Rosie Red", CellNumber = _yourVerifiedNumber, vips = new 목록<문자열> { "customer::6", "customer::7", "customer::8" } }); |
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:
1 2 3 4 5 6 |
{ "url": "https://api.twilio.com/2010-04-01/Accounts/< twilio user name here >/Messages.json", "username": "< twilio user name here >", "비밀번호": "< twilio password name here >", "fromNumber": "< twilio 'from' number here >" } |
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.
1 2 3 4 |
콘솔.WriteLine("1 - Simulate a VIP ticket scan."); 콘솔.WriteLine("2 - Simulate a regular joe ticket scan."); 콘솔.WriteLine("Q - End simulation."); var choice = 콘솔.ReadKey().KeyChar; |
A ticketscan document contains only three fields: the ID of the customer document, a timestamp, and a seat number.
1 |
_버킷.Upsert(ticketScanId, new {CustomerId = customerId, 타임스탬프 = ticketScanTimestamp, 좌석 = seatInformation }); |
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.
그리고 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.
1 |
만약 (메타.id.indexOf("ticketscan::") !== -1) { |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
let customerId = doc.customerId; let stmt = 선택 t.cellNumber, t.이름 FROM 티켓 t 어디 ANY v IN t.vips 만족 v == $customerId END; //get the concierge (there should only be one) let concierge = null; 에 대한 (var 기록 의 stmt) { concierge = 기록; } // only proceed with notification if // the customer has a concierge 만약 (concierge) { |
3 – Get the details of the VIP.
1 |
let 고객 = src[customerId]; |
4 – Get Twilio credentials.
1 |
let twilioCredentials = src["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.
1 2 3 |
let 메시지 = "Hello '" + concierge.이름 + "'. A VIP assigned to you just checked in. '" + 고객.이름 + "' will be in '" + doc.seat + "'"; let 에서 = twilioCredentials.fromNumber; let 데이터 = "To=" + concierge.cellNumber + "&From=" + 에서 + "&Body=" + 메시지; |
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.
1 2 3 |
let URL = twilioCredentials.URL; let auth = twilioCredentials.사용자 이름 + ": " + twilioCredentials.비밀번호; var 결과 = curl(URL, { "데이터": 데이터, "header": ["Content-Type: x-www-form-urlencoded"], "method": "POST", "auth": auth }); |
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:
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:
- Nifi Processing and Flow with Couchbase Server – This is how the Reds are actually going to put ticket scan data into Couchbase.
- 카우치베이스 및 Knowi를 사용한 데이터 시각화 – This is another use case that the Reds are looking at using ticket scan data for.
Please leave a comment below or find me on 트위터 @mgroves. Go Reds!
Nice blog post and good timing of the blog!
How to log/debug errors in the function. Basically if it doesnt work – where to debug.
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).