pingpong
v1.0.1
Published
Dead simple RPC with response support
Downloads
5
Readme
pingpong.js
Dead simple RPC with response support.
Install on Node
npm install pingpong
What it does
Allows a client side function to invoke a server side function with some arguments and an optional callback. The server can use the callback to send a reply or an error to the client.
Usage
Server:
var pingpong = require('pingpong');
pingpong.server(8000, function (err, server) {
server.onConnect(function (client) {
client.onMessage(function (text, responder) {
responder(null, text.toUpperCase());
});
});
});
Client:
var pingpong = require('pingpong');
pingpong.client({ port : 8000 }, function (err, remote) {
remote.invoke('Hello world!', function (err, result) {
console.log(result);
});
});
Prints HELLO WORLD!
.
Run echo.sh
from within the examples directory to see it in action.
API
server(port, callback)
: Creates a server. Invokescallback
with(err, server)
.client(config, callback)
: Creates a new client. The config is passed tonet.connect
. Invokescallback
with(err, remote)
.bind(socket, handler)
: Binds a message handler to the given socket and returns an invoker. The message handler will receive messages and invoker can be used to invoke functions on the other side.
Server API
server
: The server returned bynet.createServer
.clients
: An array of clients.onConnect(callback)
: Sets the connection handler. On new client connections,callback
is invoked withclient
.onDisconnect(callback)
: Sets the disconnct handler. On client disconnects,callback
is invoked withclient
.
Server side client API
socket
: Thenet.Socket
of the client.onMessage(callback)
: Sets the client message handler. On new client messages,callback
is invoked with the arguments that where passed in on the client side. If a client passes in a callback as the last argument toinvoke
, it is passed in to the handler and allows to send a reply.invoke(...[, callback])
: Invokes theonMessage
callback of thie client with the given arguments. If a callback is given, it will be invoked with(err, reply)
.
Client side remote API
socket
: The socket as returned bynet.connect
.invoke(...[, callback])
: Invokes theonMessage
callback for this client on the server with the given arguments. If a callback is given, it will be invoked with(err, reply)
.onMessage(callback)
: Sets the server message handler. On new server messages,calback
is invoked with the arguments that where passed in on the server side. If a server passes in a callback as the last argument toinvoke
, it is passe in to hte handler and allows to send a reply.
Compatibility
Node 0.10, 0.12
License
MIT