Lucius
Seneca Microservices in the Browser
Status - Experimental
API
Instantiation
Call lucius
to get a Seneca-like instance
act add use
Example
var seneca =
Options
get
: remote requests to made with GET, instead of POST, default:false
api
: remote endpoint, where placeholder params correspond to pattern keys, default '/api/:role/:cmd'. Set tofalse
to disable remote lookups.local
: look for patterns registered browser-side, set tofalse
to only make remote queries, default:true
Methods
add
seneca
Registers a pattern that will be matched browser-side only
act
seneca
Triggers an action that will first match against any registered
browser side patterns (as registered with add
), then may hit
a server endpoint as defined by the api
option.
use
Register a transport, see Transports
Special Patterns
$get: true
seneca
In cases where a server endpoint is hit (see api
option and seneca.act
),
adding $get: true
to the input args will force the request to be made via GET,
and the args will not be sent to the server.
$post: true
seneca
In cases where a server endpoint is hit (see api
option and seneca.act
),
adding $get: true
to the input args will force the request to be made via POST,
and the args will be sent to the server.
By default all requests are POST requests, this is only necessary when
the instance was created with the get
option set to true
.
Transports (advanced)
Custom transports open up a whole load of possibilities, for instance WebSockets, WebRTC data channels, engine.io, optimized multiplexing over long-polling HTTP, using WebWorkers in some awesome way, and so forth.
In Lucius a transport is conceptually a middleware function, which is registered with the use
function.
The responsibility of the function is to lookup the pattern supplied by args, and then call the callback with a response.
If there is no match, an error object should be supplied with a code
property
of 400
and above. For instance:
{ }
Internally, Lucius has two transports local
and http
, if there are no local
patterns http
will be checked (providing api
isn't false
).
Once the internal transports are checked, if no pattern has been found custom transports will then be checked in order of registration.
The transport can also have a static add
method attached to it,
{ /* ... etc ... */ }myTransport { }
In a server-client scenario there's little need for this behaviour, in fact it's probably a really bad idea. However, in a client-side p2p situation this could be quite important.
License
MIT
Acknowledgements
- Sponsored by nearForm