With OpenFaaS you can package anything as a serverless function - from Node.js to Golang to CSharp, even binaries like ffmpeg or ImageMagick.
NATS Server is a simple, high performance open source messaging system for cloud native applications, IoT messaging, and microservices architectures.
It's not always necessary to always trigger your functions using HTTP. Sometimes we would like to trigger functions by events. NATS Provides a messaging layer between your functions and the wider internet. By using a messaging layer we can queue our function calls allowing us to trigger out functions from events happening in your application.
Traditionally when we wrote code our backend was one application backed by a database of some sorts. As we move to a more distributed application architecture not all of our services will be served by a single database. Using events we can abstract the database away from the calling application and use a series of serverless functions, web servers, containers and third party services without maintaining a single database for the whole application.
You deploy this container or pod inside your OpenFaaS Namespace. Connect up your NATS cluster by providing an environment variable and you are ready to start triggering events!
Kubernetes
Docker Swarm
Our functions are triggered from a function.call topic we publish to NATS. See the example nats-pub*.rb files.
When publishing a message to the queue we use the following format.
Synchronous Functions
{
function: 'figlet',
params: {
body: 'Hello World'
}
}
Asynchronous Functions
{
function: 'figlet',
params: {
callback_url: 'http://your-callback.url/example'
body: 'Hello World'
}
}
The NATS Client container is smart enough to distinguish between asynchronous calls and synchronous calls based on the callback_url. If it is present we will perform an async function call, else we perform a synchronous call.
Its as simple as that.
Kubernetes
Deploy the deployment/k8s/deployment.yml to your openfaas namespace
$ kubectl apply -f deployment/k8s/deployment.yml
Docker
$ docker run -d -e NATS_HOST=nats://nats:4222 \
-e FAAS_HOSt=http://faas:8080 \
affixxx/openfaas-nats-connector
- Handle OpenFaaS Gateway Authentication
- Handle NATS Authentication
- Go Rewrite
- Deploying to Docker Swarm