Deploying Functions
Deploying functions to Nex is just as easy as deploying services. The pattern is the same for both WebAssembly and JavaScript type functions.
Function Triggers
With Nex functions you can specify a list of trigger subjects (which can include wildcards) used to activate them. So let's say you've deployed a calculator service, you may have chosen calc.*
as the trigger subject. This means that when a message comes in on a subject like calc.add
, your function will be called. It will be passed the subject calc.add
and the payload supplied on the core NATS message.
If your function returns a payload, and you used a request (instead of publish) to trigger the function, that return payload will be supplied as the response body.
While the subject trigger mechanism is incredibly flexible and powerful, we are actively thinking of additional ways we might be able to trigger functions, such as pull consumers on streams, watchers on K/V or object stores, etc.
Deploying JavaScript Functions
Let's deploy our JavaScript function. We're going to use the trigger subject js.echo
so we can differentiate from the WebAssembly function. Issue the following command (your path to the JavaScript file will likely be different):
Let's make sure the function is alive and can be triggered on the right subject:
And let's make sure the workload is visible on the node (your node ID will be different):
Everything's working as intended. Great!
Deploying WebAssembly Functions
Now let's deploy our WebAssembly function. If you didn't build yours locally, there's a downloadable echofunction.wasm
in the examples
folder in the Github repository.
Deploying this file works the same way as deploying the JavaScript function:
Now we should be able to trigger the function on the wasm.echo
subject:
As expected, we got the payload concatenated with the trigger subject wasm.echo
. We should be able to run the nats node info
command again and see both of our function workloads:
Congratulations, you've now used Nex to deploy full services compiled as static binaries, JavaScript functions, and WebAssembly functions. Deploying your applications as a combination of services and functions with Nex is fast, easy, and sets you up to joyfully deploy distributed applications.
Last updated