Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
fed135 committed Mar 3, 2017
0 parents commit b5209ec
Show file tree
Hide file tree
Showing 42 changed files with 2,663 additions and 0 deletions.
37 changes: 37 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/src/app/system/console/colors.package.js~
/package.json~
/docs/RULES.md~
# Logs
logs
*.log

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules

# Compiled api docs
api_docs
/log.txt
/bin
/stacks.out
/out
5 changes: 5 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
tests
README.md
.travis.yml
webpack.config
.jshintrc
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
language: node_js
node_js:
- "6.9"
script: "npm run-script test && node tests/benchmarks/index.js"
148 changes: 148 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<h1 align="center">
<a title="The socket optimizer" href="http://kalm.js.org">
<img alt="Kalm" width="320px" src="http://res.cloudinary.com/kalm/image/upload/v1487202241/kalm_header.png" />
<br/><br/>
</a>
Kalm
</h1>
<h3 align="center">
The Socket Optimizer
<br/><br/><br/>
</h3>
<br/>

[![Kalm](https://img.shields.io/npm/v/kalm.svg)](https://www.npmjs.com/package/kalm)
[![Node](https://img.shields.io/badge/node->%3D4.0-blue.svg)](https://nodejs.org)
[![Build Status](https://travis-ci.org/fed135/Kalm.svg?branch=master)](https://travis-ci.org/fed135/Kalm)
[![Dependencies Status](https://david-dm.org/fed135/Kalm.svg)](https://www.npmjs.com/package/kalm)
[![Gitter](https://img.shields.io/gitter/room/fed135/kalm.svg)](https://gitter.im/fed135/Kalm)


## Still under development - check out the current release [here](https://github.com/fed135/Kalm)

---

- **Easy-to-use syntax** and feature parity for all protocols
- Flexible and extensible, load your own transports and serializers
- **Multiplexing, session stores and transactions**
- Can be used between servers or in the **browser**
- Lower resource footprint and over **50x better throughtput** than plain sockets


## How it works

**Bytes transfered**

Call buffering can reduce payload sizes at the cost of some initial latency.
This makes a huge difference when you need to send a large number of small packets, such as multiplayer games do. See [Nagle's algorithm](https://en.wikipedia.org/wiki/Nagle's_algorithm).


## Usage

**Server**

```node
const Kalm = require('kalm');

// Listening for incoming UDP transactions on port 6000
const server = Kalm.listen({
port: 6000
});

server.on('connection', (client) => {
// Subscribe to 'user.action' channel
client.subscribe('user.action', (req) => {
/*
req.body The body of the request
req.client The connection handle reference
req.frame The details of the network frame
req.session The session store for that connection
*/
});

// Broadcast to all connections subscribed to the channel 'user.join'
server.broadcast('user.join', { foo: 'bar' });
});

```

**Client**

```node
import Kalm from 'kalm';

// Opens a connection to the server
// Port, transport and serial settings should match
const client = Kalm.connect({
hostname: '0.0.0.0', // Server's IP
port: 6000 // Server's port
});

client.write('user.action', {body: 'This is an object!'});
client.subscribe('user.join', () => { //... });

```
## Options
**Transports**
Name | Module
--- | ---
IPC | `Kalm.transports.IPC`
TCP | `Kalm.transports.TCP`
UDP | `Kalm.transports.UDP`
WebSockets | [kalm-websocket](https://github.com/fed135/kalm-websocket)
**Serializers**
Name | Module
--- | ---
JSON | `Kalm.serials.JSON`
MSG-PACK | [kalm-msgpack](https://github.com/fed135/kalm-msgpack)
Snappy | [kalm-snappy](https://github.com/fed135/kalm-snappy)
`null` | As-is
**Profiles**
Name | Module | Condition
--- | --- | --- |
dynamic | `Kalm.profiles.dynamic()` | Triggers based on buffer size and maximum time range (default) `{ step: 16, maxBytes: 1400 }`
heartbeat | `Kalm.profiles.heartbeat()` | Triggers at a fixed time interval `{ step: 16, maxBytes: null }`
threshold | `Kalm.profiles.threshold()` | Triggers when buffer reaches a certain size `{ step: null, maxBytes: 1400 }`
manual | `Kalm.profiles.manual()` | Need to process queues by hand `{ step: null, maxBytes: null }`
**Loading transports, profiles and serializers**
```node
// Custom adapter loading example
const Kalm = require('kalm');
const ws = require('kalm-websocket');
const msgpack = require('kalm-msgpack');

const server = Kalm.listen({
port: 3000,
transport: ws,
serial: msgpack,
profile: Kalm.profiles.heartbeat({ step: 5 }) // Triggers every 5ms
});
```
## Testing
**Unit + Smoke tests**
`npm test`
**Benchmarks**
`node tests/benchmarks`
## Logging
Kalm uses [debug](https://github.com/visionmedia/debug)
`export DEBUG=kalm`
13 changes: 13 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Kalm entry point
*/

'use strict';

/* Requires ------------------------------------------------------------------*/

const Kalm = require('./src');

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

module.exports = Kalm;
47 changes: 47 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"name": "kalm",
"version": "2.0.0",
"description": "The socket optimizer",
"main": "./index.js",
"scripts": {
"test": "mocha tests/unit --recursive && mocha tests/integration"
},
"repository": {
"type": "git",
"url": "git+https://github.com/fed135/Kalm.git"
},
"keywords": [
"socket",
"tcp",
"udp",
"client",
"server",
"service",
"peer",
"micro-service",
"low-latency",
"light",
"ipc",
"messaging",
"queue"
],
"author": "frederic charette <fredericcharette@gmail.com>",
"license": "Apache-2.0",
"bugs": {
"url": "https://github.com/fed135/Kalm/issues"
},
"homepage": "https://github.com/fed135/Kalm#readme",
"devDependencies": {
"chai": "^3.5.0",
"mocha": "^3.1.0",
"sinon": "^1.17.0"
},
"dependencies": {
"debug": "2.6.x"
},
"browser": {
"net": false,
"fs": false,
"dgram": false
}
}
50 changes: 50 additions & 0 deletions src/clientFactory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* Client Factory
*/

'use strict';

/* Requires ------------------------------------------------------------------*/

const EventEmitter = require('events').EventEmitter;
const crypto = require('crypto');

const profiles = require('./profiles');
const serials = require('./serials');
const transports = require('./transports');

const Queue = require('./components/queue');
const Multiplex = require('./components/multiplex');
const Client = require('./components/client');

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

function create(options) {
const client = {
port: 3000,
hostname: '0.0.0.0',
transport: transports.TCP,
serial: serials.JSON,
secretKey: null,
profile: profiles.dynamic(),
channels: {},
backlog: []
};

Object.assign(client,
options,
Multiplex(client),
Queue(client),
Client(client),
EventEmitter.prototype
);

client.socket = client.socket || client.transport.createSocket(client);
client.transport.attachSocket(client.socket, client);
return client;
}


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

module.exports = { create };
Loading

0 comments on commit b5209ec

Please sign in to comment.