wilton.js  v202103141
 All Namespaces Functions
Functions
DelayedResponse Namespace Reference

wilton/DelayedResponse
Send HTTP response to client from another thread More...

Functions

Object DelayedResponse (Number responseWriterHandle, Function|Undefined callback)
 Create DelayedResponse instance.
Undefined send (String|Object data, Object|Undefined options, Function|Undefined callback)
 Send string or JSON response to client.

Detailed Description

This module allows to send a "delayed" HTTP response to client. To create a DelayedResponse instance, first responseWriterHandle needs to be obtained inside the Server request handler (view) using the req.sendResponseLater()function.

DelayedResponse may be created from any thread specifying the responseWriterHandle as an argument.

Usage example:

// inside server request handler
GET: function(req) {
// delay sending a response
// and release the current worker thread
// creating a responseWriterHandle
var handle = req.sendResponseLater();
// pass the handle to other thread using some channel
chan.send({
handle: handle
});
}
// in other thread
var obj = chan.receive();
var writer = new DelayedResponse(obj.handle);
writer.send({
foo: "bar"
});

Function Documentation

Object DelayedResponse::DelayedResponse ( Number  responseWriterHandle,
Function|Undefined  callback 
)

Creates DelayedResponse object instance that can be used to send HTTP response to client from the different thread.

Parameters
responseWriterHandleNumber handle value, that must be obtained inside request handler (view) using req.sendResponseLater() call and (optionally) be passed to another thread after that
callbackFunction|Undefined callback to receive result or error
Returns
Object DelayedResponse instance
Undefined DelayedResponse::send ( String|Object  data,
Object|Undefined  options,
Function|Undefined  callback 
)

Sends response to client converting specified object into JSON if necessary (Content-Type is set to application/json in this case).

Parameters
dataString|Object response body, object will be converted to JSON
optionsObject|Undefined configuration object, see possible options below
callbackFunction|Undefined callback to receive result or error
Returns
Undefined

Options

  • meta Object response metadata
    • statusCode Number HTTP status code
    • statusMessage String HTTP status message
    • headers Object response headers in "Header-Name": "value" format