Skip to content

Commit

Permalink
Merge pull request #12 from fed135/es6-rewrite
Browse files Browse the repository at this point in the history
0.2
  • Loading branch information
fed135 committed Apr 19, 2016
2 parents 6a77dff + 730de90 commit 438d964
Show file tree
Hide file tree
Showing 28 changed files with 930 additions and 558 deletions.
9 changes: 0 additions & 9 deletions .codeclimate.yml

This file was deleted.

2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,5 @@ node_modules
# Compiled api docs
api_docs
/log.txt
/bin/kalm.min.js
/bin/kalm.min.js.map
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
language: node_js
node_js:
- "5.0"
script: "npm run-script test"
script: "npm run-script test && node tests/benchmarks/index.js"
129 changes: 68 additions & 61 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
<img align="left" src="http://i231.photobucket.com/albums/ee109/FeD135/kalm_logo.png">
# Kalm
*The Socket Optimizer*

[![Kalm](https://img.shields.io/npm/v/kalm.svg)](https://www.npmjs.com/package/kalm)
[![Build Status](https://travis-ci.org/fed135/Kalm.svg?branch=master)](https://travis-ci.org/fed135/Kalm)
[![Code Climate](https://codeclimate.com/github/fed135/Kalm/badges/gpa.svg)](https://codeclimate.com/github/fed135/Kalm)
[![Dependencies Status](https://david-dm.org/fed135/Kalm.svg)](https://www.npmjs.com/package/kalm)
[![Current Stage](https://img.shields.io/badge/stage-alpha-blue.svg)](https://codeclimate.com/github/fed135/Kalm)
[![Current Stage](https://img.shields.io/badge/stage-beta-blue.svg)](https://codeclimate.com/github/fed135/Kalm)

---

A library to simplify and optimize your Socket communications.
Simplify and optimize your Socket communications with:

- Packet bundling
- Packet minification
- Packet bundling and minification
- Easy-to-use single syntax for all protocols
- Channels for all protocols
- Plug-and-play
- Ultra-flexible and extensible
- Event channels for all protocols
- Ultra-flexible and extensible adapters

---


## Installation
Expand All @@ -24,92 +26,101 @@ A library to simplify and optimize your Socket communications.

## Usage

**Server**

```node
var Kalm = require('Kalm');

var client = new Kalm.Client({
hostname: '0.0.0.0', // Some ip
port: 3000, // Some port
adapter: 'tcp',
// Create a server, a listener for incomming connections
var server = new Kalm.Server({
port: 6000,
adapter: 'udp',
encoder: 'msg-pack',
channels: {
'myEvent': function(data) {} // Handler
messageEvent: function(data) { // Handler - new connections will register to these events
console.log('User sent message ' + data.body);
}
}
});

client.send('myEvent', {foo: 'bar'}); // Can send Objects, Strings or Buffers
client.channel('someOtherEvent', function() {}); // Can add other handlers dynamically
// When a connection is received, send a message to all connected users
server.on('connection', function(client) { // Handler, where client is an instance of Kalm.Client
server.broadcast('userEvent', 'A new user has connected');
});

```

var server = new Kalm.Server({
port: 6000,
adapter: 'udp',
encoder: 'json',
**Client**

```node
// Create a connection to the server
var client = new Kalm.Client({
hostname: '0.0.0.0', // Server's IP
port: 6000, // Server's port
adapter: 'udp', // Server's adapter
encoder: 'msg-pack', // Server's encoder
channels: {
'myEvent': function(data) {} // Handler - new connections will register to these events
'userEvent': function(data) {
console.log('Server: ' + data);
}
}
});

server.on('connection', function(client) {} // Handler, where client is an instance of Kalm.Client
server.broadcast('someOtherEvent', 'hello!');
client.send('messageEvent', {body: 'This is an object!'}); // Can send Objects, Strings or Buffers
client.channel('someOtherEvent', function() {}); // Can add other handlers dynamically

```

## Performance analysis

### Requests per minute
**Requests per minute**

| | IPC | TCP | UDP | Web Sockets |
|---|---|---|---|---|
| Raw | 1332330 | 844750 | 822690 | - |
| Kalm | 5558920 | 1102570 | 5219490 | - |
| **Result** | +417.2% | +30.5% | +634.5% | - |
<img src="http://i231.photobucket.com/albums/ee109/FeD135/perf.png">

*Benchmarks based on a single-thread queue test with Kalm default bundling settings AND msg-pack enabled*

*5 run average*
**Bytes transfered**

### Bytes transfered
<img src="http://i231.photobucket.com/albums/ee109/FeD135/transfered.png">

| | IPC | TCP | UDP | WebSockets |
|---|---|---|---|---|
| Raw | 81000 | 81000 | 57000 | - |
| Kalm | 6759 | 6759 | 8601 | - |
| **Result** | 11.9x less | 11.9x less | 6.6x less | - |

*Using wireshark - number of bytes transfered per 1000 requests*
*Number of bytes transfered per 1000 requests*


## Adapters

Allow you to easily use different socket types, hassle-free

| **Type** | **Library used** | **Status** |
|---|---|---|
| IPC | | STABLE |
| TCP | | STABLE |
| UDP | | STABLE |
| [kalm-websocket](https://github.com/fed135/kalm-websocket) | [socket.io](http://socket.io/) | DEV |
- ipc (bundled)
- tcp (bundled)
- udp (bundled)
- [kalm-websocket](https://github.com/fed135/kalm-websocket)


## Encoders

Encode the payloads before emitting.
Encodes/Decodes the payloads

| **Type** | **Library used** | **Status** |
|---|---|---|
| JSON | | STABLE |
| MSG-PACK | [msgpack-lite](https://github.com/kawanet/msgpack-lite) | STABLE |
- json (bundled)
- msg-pack (bundled)


## Middleware
## Loading custom adapters

Perform batch operation of payloads.
The framework is flexible enough so that you can load your own custom adapters, encoders or middlewares - say you wanted support for protocols like zmq, WebSockets or have yaml encoding.

| **Type** | **Library used** | **Status** |
|---|---|---|
| Bundler | | STABLE |
```node
// Custom adapter loading example
var Kalm = require('Kalm');
var MyCustomAdapter = require('my-custom-adapter');

---
Kalm.adapters.register('my-custom-adapter', MyCustomAdapter);

The framework is flexible enough so that you can load your own custom adapters, encoders or middlewares - say you wanted support for protocols like zmq, WebSockets or have yaml encoding.
var server = new Kalm.Server({
port: 3000,
adapter: 'my-custom-adapter',
encoder: 'msg-pack'
});
```


## Run tests
Expand All @@ -119,11 +130,9 @@ The framework is flexible enough so that you can load your own custom adapters,

## Debugging

By default, all Kalm logs are absorbed. They can be enabled through the DEBUG environement variable. See [debug](https://github.com/visionmedia/debug) for more info.

Ex:
By default, all Kalm logs are hidden. They can be enabled through the DEBUG environement variable. See [debug](https://github.com/visionmedia/debug) for more info.

$ DEBUG=kalm
export DEBUG=kalm


## Roadmap
Expand All @@ -135,5 +144,3 @@ Ex:

I am looking for contributors to help improve the codebase and create adapters, encoders and middleware.
Email me for details.

Thank you!
6 changes: 0 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@
/* Requires ------------------------------------------------------------------*/

var Kalm = require('./src');
var pkg = require('./package');

/* Init ----------------------------------------------------------------------*/

Kalm.Client.pkg = pkg;
Kalm.Server.pkg = pkg;

/* Exports -------------------------------------------------------------------*/

Expand Down
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"name": "kalm",
"version": "0.1.2",
"version": "0.2.0",
"description": "The socket optimizer",
"main": "./index.js",
"scripts": {
"test": "mocha tests/index.js"
"test": "mocha tests/index.js",
"build": "webpack -p -d -j --define process.env.NODE_ENV='\"browser\"' --config ./webpack.config"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -33,8 +34,6 @@
"homepage": "https://github.com/fed135/Kalm#readme",
"devDependencies": {
"chai": "3.4.x",
"coveralls": "2.11.x",
"istanbul": "0.4.x",
"mocha": "2.4.x"
},
"dependencies": {
Expand Down
99 changes: 99 additions & 0 deletions src/Channel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/**
* Channel class
* @class Channel
* @exports {Channel}
*/

'use strict';

/* Methods -------------------------------------------------------------------*/

class Channel {

/**
* Channel constructor
* @constructor
* @param {Socket} socket An optionnal socket object to use for communication
* @param {object} options The configuration options for the client
*/
constructor(name, options, client) {
this.name = name;
this.options = options;

this._client = client;
this._emitter = client._emit.bind(client);

this._timer = null;
this._packets = [];
this._handlers = [];
}

/**
* Tells the channel to process the payload to send
* @method send
* @memberof Channel
* @param {object|string} payload The payload to process
*/
send(payload) {
this._packets.push(payload);

// Bundling process
if (this._packets.length >= this.options.maxPackets) {
if (this._timer !== null) {
clearTimeout(this._timer);
this._timer = null;
}

this._emit();
return;
}

if (this._timer === null) {
this._timer = setTimeout(this._emit.bind(this), this.options.delay);
}
}

/**
* Alerts the client to emit the packets for this channel
* @private
* @method _emit
* @memberof Channel
*/
_emit() {
this._emitter(this.name, this._packets);
this._packets.length = 0;
}

/**
* Adds a method that listens to this channel
* @method addHandler
* @memberof Channel
* @param {function} method The method to bind
*/
addHandler(method) {
this._handlers.push(method);
}

/**
* Handles channel data
* @method handleData
* @memberof Channel
* @param {array} payload The received payload
*/
handleData(payload) {
var _reqs = payload.length;
var _listeners = this._handlers.length;
var i;
var c;

for (i = 0; i<_reqs; i++) {
for (c = 0; c<_listeners; c++) {
this._handlers[c](payload[i], this._client);
}
}
};
}

/* Exports -------------------------------------------------------------------*/

module.exports = Channel;
Loading

0 comments on commit 438d964

Please sign in to comment.