Martini
Client and Server RPC
Martini can generate both the client, and server from a single protocol.
Usage
Define a protocol
Create a user
type,
and define a single route that returns a JSON-encoded user.
var protocol = types: user: type: 'struct' props: name : 'string' phone : 'string,' age : 'number' require: 'name' routes: getUser: proto: method : 'GET' route : '/user/:uid' output: 'user'
Create the server
Create a server class, and implement all methods in the protocol. Each route must return a Promise object.
{} Serverprototype { return { }}
Create a router out of the server object.
var http = var RPC = var rpc = RPCvar router = rpc var server = http server
Create the client
var RPC = var rpc = RPCvar client = rpc client
Advanced
Streaming JSON
Newline delimited JSON is a powerful, yet simple protocol.
Define a typed stream using emitter/TYPE
where TYPE
is a marshalable format, e.g.
var protocol = types: user: type: 'struct' props: name: 'string' routes: getUsers: proto: method : 'GET' route : '/users' output: 'emitter/user'
Create a server route that returns an event emitter
Serverprototype { var ee = return ee}
Create a client that consumes the event emitter
client
This will print the following
Got User: bob
Got User: kim
DONE
See Also
- lib-schema the schema protocol used to validate requests
- lib-marshal the plumbing for lib-schema