Remote middleware allows binding to the project API and modifying requests from the user in an asynchronous fashion. In this way it is possible to act as express middleware over websockets in real time and reject the request, modify it or move the request to a third party service. This allows you to extend the <form.io> API platform with your own services in real-time.
Command Line Binding
The easiest way to get started with our Remote Middleware technology is to use our Command Line interface tool to help establish a websocket connection to the form you are interested in binding to. You can download the Command Line Interface by typing the folling within your terminal.
npm install -g formio-cli
Once you have this installed, you will be using the
bind method to create the websocket connection between your terminal
and the remote API platform. The format for the
bind method is as follows.
formio bind [method] [url] [middleware]
Where the following parameters need to be provided.
- [method] - The HTTP method you wish to bind yourself to. “GET”, “PUT”, “POST”, or “DELETE”
- [url] - The API endpoint for the form you wish to bind.
Once executed, the Command Line Interface will ask you for your credentials of the service to verify that you have the appropriate permissions to bind to the desired endpoint.
It should be noted, that if you omit the
middleware parameter, that the command line interface will print out the RAW
JSON data to the terminal. This can be used to pipe this data into another application. As an example, lets say we wish to
bind to our “user” resource form as follows.
formio bind POST https://yourapp.form.io/user
This would then show the following once you created a new User into the resource table.
Custom Remote Middleware
Not only can you bind to an API via command line, but you can also execute a middleware file which can gain direct, real-time access to the requests as they are coming into Form.io. This can be done by creating a Node.js middleware file that has a similar syntax to Express.js middleware.
This file could then be bound to the Form as follows within the command line.
formio bind POST https://yourapp.form.io/user ./middleware.js
Now when you submit a new submission into that form, this middleware will be executed remotely, and then altered in real-time.
We think it is pretty amazing too.
Node.js Remote Middleware
Remote middleware can also be utilized within a Node.js application using the popular module Primus. We have included a handy wrapper for our binding capabilities within our Form.io Service library. You can include this library within your Node.js application using the following.
npm install -g formio-service
Once you have this within your application, you can bind to a Form.io form using the following code.