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.


The Couchbase Eventing service is new as of Couchbase 5.5 and I’d previously written a tutorial about detecting sensitive information and sending notifications. In my previous tutorial I demonstrated to how make requests to a remote service using the cURL operator in N1QL. However, what if you didn’t want to use N1QL to get the job done?

We’re going to see how to use the cURL function to make requests rather than N1QL when using the Eventing service.

As of right now Eventing is in Developer Preview, so functionality may be subject to change. However, this should give you an idea on what you can do.

Please note, the curl construct is still in Development. This feature is intended for Development purposes only and should not be used in Production environments.

Rather than trying to find an API to use, we’re going to create our own with Node.js, similar to what we did in the previous example. Once we have the API in place, we’re going to create a function in Couchbase that will trigger when new documents are created or changed.

Creating a Node.js API for Receiving Requests

Our API is going to be incredibly simple because Node.js is not the focus of this tutorial. We’re going to receive data and return that same data back to our function.

Create a new directory on your computer and execute the following commands from the CLI within that directory:

The above commands will create a newĀ package.json file and install Express Framework and a module for accepting request bodies. With the dependencies available, execute the following:

If you’re on Windows, go ahead and create theĀ app.js file however you want.

Open the newly createdĀ app.js file and include the following JavaScript code:

We have a single endpoint that does exactly as I previously mentioned. When we run this application, it is serving on port 3000. For our example, both the database and the Node.js application will be running locally. If things are different, make sure the Node.js application is accessible from a remote host.

Developing a Simple Couchbase Function with cURL Support

Our function will be very simple for this example. We won’t be checking for certain data, we’ll only be checking to see if something was created or changed. If the function triggers, we’ll post the data to our Node.js application.

In theĀ Eventing section of the Couchbase Administrative Dashboard, create a new function, mapping the correct Bucket and meta information Bucket. The code we want to add looks like the following:

Notice that we’re using the curl function and specifying some options such as the request body, and the method. If we wanted to we could add other properties such as headers or authorization.

Try adding a new document or changing one. The document should send.

Conclusion

You just saw how to send data to a remote service from a Couchbase Function with the cURL function and the Couchbase Eventing service. This is a little different than my previous example because it doesn’t use N1QL to make this possible.

To learn more about the Couchbase Eventing service, check out the Couchbase Developer Portal.

Author

Posted by Nic Raboy, Developer Advocate, Couchbase

Nic Raboy is an advocate of modern web and mobile development technologies. He has experience in Java, JavaScript, Golang and a variety of frameworks such as Angular, NativeScript, and Apache Cordova. Nic writes about his development experiences related to making web and mobile development easier to understand.

Leave a reply