https://github.com/jutaz/dnode-tarantula
Bi-directional RPC for node.js
https://github.com/jutaz/dnode-tarantula
Last synced: 6 months ago
JSON representation
Bi-directional RPC for node.js
- Host: GitHub
- URL: https://github.com/jutaz/dnode-tarantula
- Owner: jutaz
- Created: 2013-11-26T10:45:50.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2014-12-29T13:23:40.000Z (over 10 years ago)
- Last Synced: 2024-11-14T09:43:24.345Z (6 months ago)
- Language: JavaScript
- Homepage:
- Size: 730 KB
- Stars: 4
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# dnode-tarantula
dnode-tarantula is an asynchronous rpc and event system for node.js based on dnode-protocol and TCP sockets.
This is fork of [dnode-spider](https://github.com/llevkin/dnode-spider)[](https://nodei.co/npm/dnode-tarantula/)
### Features
* Automatic reconnection
* bi-direction and poly-direction communication
* Events### Install
```
npm install dnode-tarantula
```### Examples
Also check [examples](/examples/)
server.js:
``` js
var dnode = require('dnode-tarantula');var server = new dnode.Server({
transform: function (a, b, cb) {
cb(a + b, 'Hello from Spider!');
}
}, {port: 5000, host: 'localhost'});server.on('connection', function(remote) {
remote.math(1, 2, function(res, hello) {
console.log(res, hello);
});
});```
client.js:
``` js
var dnode = require('dnode-tarantula');var client = new dnode.Client({
math: function (a, b, cb) {
cb((a + b) * 2, 'Hello from Fly! My name: '+client.nodeId);
}
}, {port: 5000, host: 'localhost', nodeId: 'Fly1'});client.on('connection', function(remote) {
remote.transform(1, 2, function(res, hello) {
console.log(res, hello);
});
});```
output:
```
node server.js &
node client.js &
3 'Hello from Spider!'
6 'Hello from Fly! My name: Fly1'
```## Methods
``` js
var dnode = require('dnode-tarantula')
```# API
## Server
``` js
var server = dnode.Server(api, options);
```* Object api - shared server methods object
* Object options - settings object```js
{
port: 1337, //default: 5000
host: 'node.example.com', // default: 'localhost'
auth: function(flyAuth, callback), // default: null
id: '1337', // any string. Default: random shortid
store: new dnode.stores.redis({
pub: redis.createServer(),
sub: redis.createServer(),
client: redis.createServer()
}), // default: memory store
pingInterval: 15000, // Any number in ms. default: 10000
}
```Api object has `$` object, which is reserved for internal stuff (Danger Zone™).
Call method with 'methodname' from Client with id = 'nodeId'.
### api.$.ids(callback)
* Function callback - callback
Return Array of all Client ID`s that are connected to Server
### server.update(id)
Update server API to client with id == id
### server.broadcast(methodname, [arguments])
* String methodname - name of method
* Array arguments - arguments, that should be passed to remote functionBroadcast a function call to all clients.
### server.ids()
Return ids of all connected clients
### Events
``` js
server.on('connection', function(remote, client, api) {}); // client connected
server.on('disconnection', function(client) {}); // client disconnected
```## Client
```js
var client = dnode.Client(api, options);
```* Object api - shared Client methods object
* Object options - settings object```js
{
port: 1337, //default: 5000
host: 'node.example.com', // default: 'localhost'
nodeId: 'W00T', //default: process.pid
auth: 'SoSecureKey' //auth key. default: null
}
```### client.update()
Call this method after updating your API object, and changes will propagate to server.
### Events
``` js
client.on('remote', function(remote) {}); // client remote methods are ready
client.on('ping', function(client) {}); // ping packet sent
client.on('ping:timeout', function(client) {}); // well... A ping timeout!
client.on('ping:reply', function(client) {}); // emitted when ping reply arrives
```