Deploying Nex 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):

  1. $ nex devrun /home/kevin/echofunction.js --trigger_subject=js.echo
  2. Reusing existing issuer account key: /home/kevin/.nex/issuer.nk
  3. Reusing existing publisher xkey: /home/kevin/.nex/publisher.xk
  4. 🚀 Workload 'echofunctionjs' accepted. You can now refer to this workload with ID: cmjud7n52omhlsa377cg on node NC7PXV2DLGXC4LTVM7W7MXYL3WVQFA345IFKJOMYA5ZDZMACLZ53NIIL

Let’s make sure the function is alive and can be triggered on the right subject:

  1. $ nats req js.echo 'heya'
  2. 09:40:33 Sending request on "js.echo"
  3. 09:40:33 Received with rtt 2.600724ms
  4. "heya"

And let’s make sure the workload is visible on the node (your node ID will be different):

  1. $ nex node info NC7PXV2DLGXC4LTVM7W7MXYL3WVQFA345IFKJOMYA5ZDZMACLZ53NIIL
  2. NEX Node Information
  3. Node: NC7PXV2DLGXC4LTVM7W7MXYL3WVQFA345IFKJOMYA5ZDZMACLZ53NIIL
  4. Xkey: XDKZMOZKVBXSY3YXPIXEFKGPML75PLD7APFHZ474EOCILZDQGPZSXJNZ
  5. Version: 0.0.1
  6. Uptime: 2m26s
  7. Tags: nex.arch=amd64, nex.cpucount=8, nex.os=linux, simple=true
  8. Memory in kB:
  9. Free: 32,354,208
  10. Available: 55,985,740
  11. Total: 63,883,232
  12. Workloads:
  13. Id: cmjud7n52omhlsa377cg
  14. Healthy: true
  15. Runtime: 2m26s
  16. Name: echofunctionjs
  17. Description: Workload published in devmode

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:

  1. $ nex devrun ../examples/wasm/echofunction/echofunction.wasm --trigger_subject=wasm.echo
  2. Reusing existing issuer account key: /home/kevin/.nex/issuer.nk
  3. Reusing existing publisher xkey: /home/kevin/.nex/publisher.xk
  4. 🚀 Workload 'echofunctionwasm' accepted. You can now refer to this workload with ID: cmjudmn52omhlsa377d0 on node NC7PXV2DLGXC4LTVM7W7MXYL3WVQFA345IFKJOMYA5ZDZMACLZ53NIIL

Now we should be able to trigger the function on the wasm.echo subject:

  1. $ nats req wasm.echo 'hello'
  2. 09:45:24 Sending request on "wasm.echo"
  3. 09:45:24 Received with rtt 42.867014ms
  4. hellowasm.echo

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:

  1. $ nex node info NC7PXV2DLGXC4LTVM7W7MXYL3WVQFA345IFKJOMYA5ZDZMACLZ53NIIL
  2. NEX Node Information
  3. Node: NC7PXV2DLGXC4LTVM7W7MXYL3WVQFA345IFKJOMYA5ZDZMACLZ53NIIL
  4. Xkey: XDKZMOZKVBXSY3YXPIXEFKGPML75PLD7APFHZ474EOCILZDQGPZSXJNZ
  5. Version: 0.0.1
  6. Uptime: 7m31s
  7. Tags: nex.arch=amd64, nex.cpucount=8, nex.os=linux, simple=true
  8. Memory in kB:
  9. Free: 32,280,180
  10. Available: 56,018,344
  11. Total: 63,883,232
  12. Workloads:
  13. Id: cmjud7n52omhlsa377cg
  14. Healthy: true
  15. Runtime: 7m31s
  16. Name: echofunctionjs
  17. Description: Workload published in devmode
  18. Id: cmjudmn52omhlsa377d0
  19. Healthy: true
  20. Runtime: 6m31s
  21. Name: echofunctionwasm
  22. Description: Workload published in devmode

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.