Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/esnunes/mar

MAR - Message & Reply - RPC library based on message exchange.
https://github.com/esnunes/mar

Last synced: about 2 months ago
JSON representation

MAR - Message & Reply - RPC library based on message exchange.

Awesome Lists containing this project

README

        

# MAR

MAR - Message & Reply - RPC library based on message exchange.

## Quick Examples

Usage examples can be found [here](http://github.com/esnunes/mar/tree/master/examples) and below.

### Server
```js
var Server = require('mar').Server;

var s = new Server(8081);
s.on('ping', function(msg) {
console.log('ping sent at [%s]', new Date(msg.data));
if (msg.reply) msg.reply(null, new Date().getTime());
});
```

### Client
```js
var Client = require('mar').Client;

var c = new Client();
c.on('connect', function ping() {
c.message('ping', new Date().getTime(), function(err, data) {
if (err) return console.log(err);
console.log('pong received at [%s]', new Date(data));
});
});
c.connect({ host: 'localhost', port: 8081 });
```

### Server - Authentication
```js
var Server = require('mar').Server;

var s = new Server(8081);
s.on('ping', function(msg) {
console.log('ping sent at [%s]', new Date(msg.data));
if (msg.reply) msg.reply(null, new Date().getTime());
});

s.on('mar.auth', function(msg) {
if (msg.data != 'foobar') return msg.reply('auth denied');
msg.reply(null, 'auth granted');
});
```

### Client - Authentication
```js
var Client = require('mar').Client;

var c = new Client();
c.on('connect', function() {
c.message('mar.auth', 'foobar', function(err, auth) {
if (err) return console.log(err);
console.log(auth);
ping();
});
});

function ping() {
c.message('ping', new Date().getTime(), function(err, data) {
if (err) return console.log(err);
console.log('pong received at [%s]', new Date(data));
});
}

c.connect({ host: 'localhost', port: 8081 });
```

## Download

The source is available for download from
[GitHub](http://github.com/esnunes/mar).
Alternatively, you can install using Node Package Manager (npm):

npm install mar

## Future - TODO

There are many thoughts about future features for MAR library. You can find them [here](http://github.com/esnunes/mar/blob/master/TODO.md).