Coding in Cloud Flow
Last updated
Last updated
For Cloud Flow, the Custom Code
block is functional in Business or higher edition apps. This block can be tested in the Standard edition.
In Cloud Flow, the block allows you to add based code for advanced custom functionality.
Your Zingy app's server-side backend includes a curated list of Node.js SDKs and Libraries that can be used within your app's Cloud Flow Web Hooks and Background Tasks using the block.
For more information about the list of integrated libraries and sdks please see .
The variables and functions that are added to the Cloud Flow (using blocks) can also be referenced by the code within the block.
support advanced options for sending response to the requesting client as detailed below:
When using the Piped (Pass-through) response streaming option, the response data needs to be provided as a to the znResponsePipe
function, the prototype for which is shown below:
readable
The stream to read from and pipe into the response
contentType
string
noAutoExit
boolean
By default, the znResponsePipe
function will auto exit the Web Hook once the stream is fully read and piped. This optional parameter allows you to disable this behavior and receive a callback notification at the function specified in the endCbFn
parameter.
endCbFn
function
When noAutoExit
is set to true, this function gets called when the stream has been read or upon error. A string parameter is passed to indicate the status.
The above example conserves your Zingy app's server-side memory by directly streaming the large CSV file, instead of reading it fully and then transferring it as part of the Web Hook's response.
The following functions facilitate this implementation:
The znResponseChunkAdd
(above), is used to send the next chunk of data to the client. The data
parameter specifies the chunk to be sent.
The znResponseChunkEnd
function, shown above, is used to signal that all chunks have been sent to the client. By default, this function will auto exit the Web Hook. The noAuthExit
parameter when set to true, prevents this and calls the function specified by the endCbFn
parameter.
Upon receiving the end event, the znResponseChunkEnd
function is called to close the response and exit the Web Hook.
The value for the HTTP in the response.
The example below shows a large CSV file being retrieved from an external source using the library. Since a responseType
of stream was specified for , it provides a at httpResp.data
, that is passed to the znResponsePipe
function. The contentType
parameter is set to text/csv.
When using the Chunked response streaming option, the response data is sent in batches (or chunks) to the requesting client. It sets the HTTP header to chunked in the response.
The znResponseChunkStart
function, shown above, is used to initiate the response to the client. The contentType
parameter specifies the value for the HTTP in the response.
The example below shows the same CSV file (as the Piped example above) being retrieved using the library. Since a responseType
of stream was specified for , it provides a at httpResp.data
, that is piped into the library.
The library, parses the stream and produces an object for each line parsed. We are notified about this via the data event. In the handler for the data event, we convert the object to a JSON string and send as a chunk using the znResponseChunkAdd
function.