smartsocket/README.md

101 lines
4.6 KiB
Markdown
Raw Normal View History

2016-08-06 21:27:53 +00:00
# smartsocket
2016-08-07 17:07:11 +00:00
easy and secure websocket communication, Typescript ready
2016-08-06 21:27:53 +00:00
2016-09-03 14:03:57 +00:00
## Availabililty
[![npm](https://push.rocks/assets/repo-button-npm.svg)](https://www.npmjs.com/package/smartsocket)
[![git](https://push.rocks/assets/repo-button-git.svg)](https://gitlab.com/pushrocks/smartsocket)
[![git](https://push.rocks/assets/repo-button-mirror.svg)](https://github.com/pushrocks/smartsocket)
[![docs](https://push.rocks/assets/repo-button-docs.svg)](https://pushrocks.gitlab.io/smartsocket/docs)
## Status for master
2016-08-06 21:27:53 +00:00
[![build status](https://gitlab.com/pushrocks/smartsocket/badges/master/build.svg)](https://gitlab.com/pushrocks/smartsocket/commits/master)
2016-09-03 14:03:57 +00:00
[![coverage report](https://gitlab.com/pushrocks/smartsocket/badges/master/coverage.svg)](https://gitlab.com/pushrocks/smartsocket/commits/master)
[![Dependency Status](https://david-dm.org/pushrocks/smartsocket.svg)](https://david-dm.org/pushrocks/smartsocket)
[![bitHound Dependencies](https://www.bithound.io/github/pushrocks/smartsocket/badges/dependencies.svg)](https://www.bithound.io/github/pushrocks/smartsocket/master/dependencies/npm)
[![bitHound Code](https://www.bithound.io/github/pushrocks/smartsocket/badges/code.svg)](https://www.bithound.io/github/pushrocks/smartsocket)
[![TypeScript](https://img.shields.io/badge/TypeScript-2.x-blue.svg)](https://nodejs.org/dist/latest-v6.x/docs/api/)
[![node](https://img.shields.io/badge/node->=%206.x.x-blue.svg)](https://nodejs.org/dist/latest-v6.x/docs/api/)
2016-08-06 21:27:53 +00:00
## Usage
We recommend the use of typescript.
2016-08-07 17:07:11 +00:00
Under the hood we use socket.io and shortid for managed data exchange.
2016-08-06 21:27:53 +00:00
2016-08-07 13:12:21 +00:00
### Serverside
2016-08-07 12:58:20 +00:00
```typescript
2016-08-07 13:14:53 +00:00
import * as smartsocket from "smartsocket";
2016-09-25 22:23:17 +00:00
import * as q from q // q is a promise library
2016-08-07 13:14:53 +00:00
2016-09-25 22:28:39 +00:00
// The "Smartsocket" listens on a port and can receive new "SocketConnection" requests.
2016-08-07 12:58:20 +00:00
let mySmartsocket = new smartsocket.Smartsocket({
port: 3000 // the port smartsocket will listen on
});
2016-09-25 22:28:39 +00:00
// A "SocketRole" can be referenced by "SocketFunction"s.
// All "SocketRequest"s carry authentication data for a specific "SocketRole".
// "SocketFunction"s know which "SocketRole"s are allowed to execute them
2016-08-07 12:58:20 +00:00
let mySocketRole = new smartsocket.SocketRole({
name: "someRoleName",
2016-08-07 13:12:21 +00:00
passwordHash: "someHashedString"
2016-08-07 12:58:20 +00:00
});
2016-09-25 22:28:39 +00:00
// A "SocketFunction" executes a referenced function and passes in any data of the corresponding "SocketRequest".
// The referenced function must return a promise and resolve with data of type any.
// Any "SocketRequest" carries a unique identifier. If the referenced function's promise resolved any passed on argument will be returned to the requesting party
2016-08-15 01:47:32 +00:00
let testSocketFunction1 = new smartsocket.SocketFunction({
funcName:"testSocketFunction1",
funcDef:(data) => {
2016-09-25 22:23:17 +00:00
console.log('testSocketFunction1 executed successfully!')
},
2016-08-15 01:47:32 +00:00
allowedRoles:[mySocketRole] // all roles that have access to a specific function
2016-08-07 12:58:20 +00:00
});
2016-09-25 22:28:39 +00:00
// A "Smartsocket" exposes a .clientCall() that gets
// 1. the name of the "SocketFunction" on the client side
2016-09-25 22:23:17 +00:00
// 2. the data to pass in
2016-09-25 22:28:39 +00:00
// 3. And a target "SocketConnection" (there can be multiple connections at once)
2016-09-25 22:23:17 +00:00
// any unique id association is done internally
mySmartsocket.clientCall("restart",data,someTargetConnection)
2016-08-07 13:12:21 +00:00
.then((responseData) => {
2016-08-07 12:58:20 +00:00
2016-08-07 13:12:21 +00:00
});
```
#### Client side
```typescript
2016-08-07 13:14:53 +00:00
import * as smartsocket from "smartsocket";
2016-09-25 22:28:39 +00:00
// A "SmartsocketClient" is different from a "Smartsocket" in that it doesn't expose any public address.
// Thus any new "SocketConnection"s must be innitiated from a "SmartsocketClient".
2016-08-15 01:47:32 +00:00
let testSmartsocketClient = new smartsocket.SmartsocketClient({
port: testConfig.port,
url: "http://localhost",
password: "testPassword",
alias: "testClient1",
role: "testRole1"
2016-08-07 12:58:20 +00:00
});
2016-09-25 22:23:17 +00:00
2016-09-25 22:28:39 +00:00
// You can .connect() and .disconnect() from a "Smartsocket"
2016-08-15 01:47:32 +00:00
testSmartsocketClient.connect()
.then(() => {
done();
});
2016-08-07 12:58:20 +00:00
2016-09-25 22:28:39 +00:00
// The client can also specify "SocketFunction"s. It can also specify "SocketRole"s in case a client connects to multiple servers at once
2016-08-15 01:47:32 +00:00
let testSocketFunction2 = new smartsocket.SocketFunction({
funcName: "testSocketFunction2",
funcDef: (data) => {}, // the function to execute, has to return promise
allowedRoles:[]
2016-08-07 12:58:20 +00:00
});
2016-09-25 22:28:39 +00:00
// A "SmartsocketClient" can call functions on the serverside using .serverCall() analog to the "Smartsocket"'s .clientCall method.
2016-08-15 01:47:32 +00:00
mySmartsocketClient.serverCall("function",functionCallData)
.then((functionResponseData) => { // the functionResponseData comes from the server... awesome, right?
2016-08-07 13:12:21 +00:00
});;
2016-08-07 17:07:11 +00:00
```
2016-08-07 17:15:05 +00:00
> **NOTE:**
2016-08-15 01:47:32 +00:00
you can easily chain dependent requests on either the server or client side with promises.
2016-08-07 17:15:05 +00:00
`data` is always a js object that you can design for your specific needs.
It supports buffers for large binary data network exchange.