Package | vein |
Description | RPC via WebSockets |
Node Version | >= 0.6 |
Vein uses ES5 features so be sure to include es5shim on your page.
var Vein = require('vein');
var server = http.createServer().listen(8080);
var vein = Vein.createServer(server);
vein.add('multiply', function (res, numOne, numTwo){
res.reply(numOne * numTwo);
});
Note: Express 3 is no longer a httpServer so you need to do something like:
var server = require('http').createServer(app).listen(8080);
before passing it to Vein.createServer
var vein = Vein.createClient();
vein.ready(function (services){
vein.multiply(2, 5, function (num){
// num === 10
});
});
-- Options --
resource - change to allow multiple servers on one port (default: "default")
var Vein = require('vein');
var vein = Vein.createServer(httpServer, {options});
Arguments passed to res.reply() will be applied to the callback on the client
vein.add('getNumber', function (res, name, num) {
res.reply("Hey there " + name + " I got your number " + num);
});
You can use middleware to add layers in front of your services. Any arguments passed into next will be thrown as an error on the client and end the middleware chain.
vein.use(function(req, res, next){
if (req.service == 'login') {
next();
} else {
if (res.cookie('login') == 'success!') {
next();
} else {
res.disconnect();
}
}
});
Vein supports calling the res object as a function. This makes it easier to integrate vanilla-JS services into vein. The only difference is that you still have to put the callback first (this is to prevent headaches with variable arguments from the client).
vein.add('echoUser', function (res, username, password) {
res(username, password);
});
-- Options --
host - server location (default: window.location.hostname)
port - server port (default: window.location.port)
secure - use SSL (default: window.location.protocol)
resource - change to allow multiple servers on one port (default: "default")
var vein = Vein.createClient({options});
When the connection has been established your callback will be called with an array of services available.
vein.on('ready', function (services) {
//Start doing stuff!
});
When calling a service the format is vein.serviceName(args..., callback)
vein.getNumber('john', 2, function (msg) {
console.log(msg);
});
If the connection has been closed this will be called.
vein.on('close', function (reason) {
console.log("Connection lost due to", reason);
});
You can view a tiny addition sample and more in the example folder.
(MIT License)
Copyright (c) 2012 Fractal [email protected]
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.